При запуске View view error: «Невозможно выполнить эту операцию, пока обработка диспетчера приостановлена» - PullRequest
1 голос
/ 04 ноября 2011

Когда я пытаюсь запустить View из конструктора чужих представлений ViewModel, я получаю сообщение об ошибке:
Невозможно выполнить эту операцию, пока обработка диспетчера приостановлена ​​

ViewModel моего вида, откуда я запускаю (UserControl)

public class ExecutionViewModel : BaseViewModel
{
    #region Member Variables
    /// <summary>
    ///   Logger
    /// </summary>
    private static readonly ILog log =
        LogManager.GetLogger(typeof(ConfigurationViewModel));
    #endregion

    #region Properties
    /// <summary>
    ///   Get/Set ExitCommand
    /// </summary>
    public ICommand ExitCommand { get; set; }

    /// <summary>
    ///   Command To Launch Report a Problem
    /// </summary>
    public ICommand ReportAProblemView { get; set; }

    /// <summary>
    ///   Command To Launch About
    /// </summary>
    public ICommand AboutView { get; set; }

            /// <summary>
    /// LaunchNavigationkeyViewCommamd
    /// </summary>
    public ICommand LaunchSelectionScreenCommamd { get; set; }


    #endregion

    # region Constructor
    /// <summary>
    ///   Default Constructor
    /// </summary>
    public ExecutionViewModel()
    {
        ExitCommand = new RelayCommand(ExitFromApplication);
        ReportAProblemView = new RelayCommand(LaunchReportAProblemView,
                                              CanLaunchReportAProblemView);
        AboutView = new RelayCommand(LaunchAboutView, CanLaunchAboutView);
        LaunchNavigationkeyViewCommamd = new RelayCommand(LaunchNavigationkeyView);
        LaunchSelectionScreenCommamd =
            new RelayCommand(LaunchSelectionScreenView);
        LaunchMenuConfigViewCommamd = new RelayCommand(LaunchMenuConfigView);
        Messenger.UnRegister(this);
        Messenger.Register(this);
        UpdateStatusBar();
        //Here i am Trying to perfrom Somecheck and Launch View if Needed
       InitiateStartUpValidations();
    }

    Private void InitiateStartUpValidations()
    {
        if (Utility.IsStringNullOrEmpty(DOEConfigFileName))
        {

            PasswordViewModel passwordViewModel = new PasswordViewModel(FileDialogModes.Open) { ShowPasswordBox = false };

            // Launching View using Messenger
            // This Gives an error
            Messenger.NotifyColleagues(MessengerMessages.LAUNCH_FILE_SELECT_VIEW, passwordViewModel);
        }
    }

    /// <summary>
    /// LaunchMenuConfigView
    /// </summary>
    /// <param name="obj"></param>
    private void LaunchMenuConfigView(object obj)
    {
        log.Debug("LaunchMenuConfigView" + BaseModel.FUNCTION_ENTERED_LOG);
        MenuConfigViewModel menuConfigViewModel =
                      new MenuConfigViewModel();
        Messenger.NotifyColleagues(MessengerMessages.LAUNCH_VIEW_MENU_CONFIG, menuConfigViewModel);
        log.Debug("LaunchMenuConfigView" + BaseModel.FUNCTION_EXIT_LOG);
    }
    /// <summary>
    /// LaunchSelectionScreenView
    /// </summary>
    /// <param name="obj"></param>
    private void LaunchSelectionScreenView(object obj)
    {
        log.Debug("LaunchSelectionScreenView" + BaseModel.FUNCTION_ENTERED_LOG);
        SelectionScreenViewModel selectionScreenViewModel =
            new SelectionScreenViewModel();
        Messenger.NotifyColleagues(MessengerMessages.LAUNCH_SELECTIONSCREEN_VIEW, selectionScreenViewModel);
        log.Debug("LaunchSelectionScreenView" + BaseModel.FUNCTION_EXIT_LOG);
    }

    # endregion

    # region Private Methods   
    /// <summary>
    /// CanLaunchSelectPageDefinitionView
    /// </summary>
    /// <param name="obj"></param>
    private void CanLaunchSelectPageDefinitionView(object obj)
    {

    }
    /// <summary>
    /// LaunchNavigationkeyView
    /// </summary>
    private void LaunchNavigationkeyView(object obj)
    {
        log.Debug("LaunchNavigationkeyView" + BaseModel.FUNCTION_ENTERED_LOG);
        UtilityViewModel utilityViewModel = new UtilityViewModel();
        Messenger.NotifyColleagues(MessengerMessages.LAUNCH_VIEW_NAVIGATIONKEY, utilityViewModel);
        log.Debug("LaunchNavigationkeyView" + BaseModel.FUNCTION_EXIT_LOG);

    }
    /// <summary>
    ///   Update the progressbar status message
    /// </summary>
    private void UpdateStatusBar()
    {
        log.Debug("UpdateStatusBar" + BaseModel.FUNCTION_ENTERED_LOG);
        UpdateProgressbarStatus(string.Empty, false);
        log.Debug("UpdateStatusBar" + BaseModel.FUNCTION_EXIT_LOG);
    }
    /// <summary>
    /// CanLaunchAboutView
    /// </summary>
    /// <param name="obj"></param>
    /// <returns></returns>
    private bool CanLaunchAboutView(object obj)
    {
        return true;
    }
    /// <summary>
    /// LaunchAboutView
    /// </summary>
    /// <param name="obj"></param>
    private void LaunchAboutView(object obj)
    {
        Messenger.NotifyColleagues(MessengerMessages.LAUNCH_ABOUT_VIEW, "About View");
    }
    /// <summary>
    /// CanLaunchReportAProblemView
    /// </summary>
    /// <param name="obj"></param>
    /// <returns></returns>
    private bool CanLaunchReportAProblemView(object obj)
    {
        return true;
    }
    /// <summary>
    /// LaunchReportAProblemView
    /// </summary>
    /// <param name="obj"></param>
    private void LaunchReportAProblemView(object obj)
    {
        Messenger.NotifyColleagues(MessengerMessages.LAUNCH_REPORT_A_PROBLEM_VIEW, "Report A Problem");
    }
    /// <summary>
    /// </summary>
    /// <param name = "obj"></param>
    private void ExitFromApplication(object obj)
    {
        log.Debug(BaseModel.FUNCTION_ENTERED_LOG);
        Messenger.NotifyColleagues(MessengerMessages.EXIT_APPLICATION, obj);
        log.Debug(BaseModel.FUNCTION_EXIT_LOG);
    }
    # endregion
}

Кодирование моего MainView (где зарегистрированы все виды)

#region class - MainView
/// <summary>
/// Interaction logic for MainView.xaml
/// </summary>
public partial class MainView : BaseWindowClass
{
    # region Member Variables
    /// <summary>
    /// log
    /// </summary>        
    private static readonly ILog log = LogManager.GetLogger(typeof(MainView));

    public readonly MainViewModel mainViewModel;
    # endregion

    # region Constructor

    /// <summary>
    /// Default Constuctor
    /// </summary>
    public MainView()
    {
        InitializeComponent();
        mainViewModel = new MainViewModel();
        mainViewModel.Messenger.UnRegister(this);
        mainViewModel.Messenger.Register(this);
        this.DataContext = mainViewModel;            
    }        
    # endregion

    # region Protected Methods
    /// <summary>
    /// LaunchMenuConfigView
    /// </summary>
    /// <param name="menuConfigViewModel"></param>
    [MessengerMessageSink(MessengerMessages.LAUNCH_VIEW_MENU_CONFIG, ParameterType = typeof(MenuConfigViewModel))]
    protected void LaunchMenuConfigView(MenuConfigViewModel menuConfigViewModel)
    {

      log.Debug("LaunchMenuConfigView" + Utility.FUNCTION_ENTERED_LOG);
      var menuConfigView = new MenuConfigView() 
      {
          DataContext = menuConfigViewModel,
          Owner =GetWindow(this),
      };
      menuConfigView.ShowDialog();
      log.Debug("LaunchMenuConfigView" + Utility.FUNCTION_EXIT_LOG);
    }
    /// <summary>
    /// LaunchNavigationKeyDetailsView
    /// </summary>
    /// <param name="obj"></param>
    [MessengerMessageSink(MessengerMessages.LAUNCH_VIEW_NAVIGATIONKEY, ParameterType = typeof(UtilityViewModel))]
    protected void LaunchNavigationKeyDetailsView(UtilityViewModel utilityViewModel)
    {
        log.Debug("LaunchNavigationKeyDetailsView" + Utility.FUNCTION_ENTERED_LOG);
        var navigationScreen = new NavigationKeyDetailsView() {DataContext=utilityViewModel,Owner=GetWindow(this) };
        navigationScreen.ShowDialog();
        log.Debug("LaunchNavigationKeyDetailsView" + Utility.FUNCTION_EXIT_LOG);
    }
    /// <summary>
    /// ReportProblemMenuItem_Click
    /// </summary>
    /// <param name="obj"></param>
    [MessengerMessageSink(MessengerMessages.LAUNCH_ABOUT_VIEW,ParameterType=typeof(string))]
    protected void LaunchABoutView(string  obj)
    {
        log.Debug("LaunchABoutView" + Utility.FUNCTION_ENTERED_LOG);
        var aboutScreen = new AboutDialog(Utility.TOOL_NAME,
                                          Utility.TOOL_VERSION);
        aboutScreen.ShowDialog();
        log.Debug("LaunchABoutView" + Utility.FUNCTION_EXIT_LOG);
    }
    /// <summary>
    /// ReportProblemMenuItem_Click
    /// </summary>
    /// <param name="obj"></param>
    [MessengerMessageSink(MessengerMessages.LAUNCH_REPORT_A_PROBLEM_VIEW, ParameterType = typeof(string))]
    protected void LaunchReportProblemView(string  obj)
    {
        log.Debug("LaunchReportProblemView" + Utility.FUNCTION_ENTERED_LOG);
        ReportProblemView objReportProblemView = new ReportProblemView
                                                     {
                                                         Owner = GetWindow(this)
                                                     };
        objReportProblemView.ShowDialog();
        log.Debug("LaunchReportProblemView" + Utility.FUNCTION_EXIT_LOG);
    }
    /// <summary>
    /// FileSelectView
    /// </summary>
    /// <param name="passwordViewModel">ViewModel Instance of <c>PasswordView</c></param>
    [MessengerMessageSink(MessengerMessages.LAUNCH_FILE_SELECT_VIEW,ParameterType=typeof(PasswordViewModel))]
    protected void LaunchFileSelectView(PasswordViewModel passwordViewModel)
    {
        log.Debug("LaunchFileSelectView" + Utility.FUNCTION_ENTERED_LOG);
        PasswordView passwordView = new PasswordView
        {
            DataContext = passwordViewModel,
            Owner = GetWindow(this)
        };
        passwordView.ShowDialog();
        log.Debug("LaunchFileSelectView" + Utility.FUNCTION_EXIT_LOG);
    }

    /// <summary>
    /// Launch Ask Password
    /// </summary>
    /// <param name="changePasswordViewModel"></param>
    [MessengerMessageSink(MessengerMessages.ASKFORPASSWORD, ParameterType = typeof(ChangePasswordViewModel))]
    protected void LaunchAskPasswordView(ChangePasswordViewModel changePasswordViewModel)
    {
        log.Debug("LaunchChangePasswordView" + Utility.FUNCTION_ENTERED_LOG);
         ChangePasswordView objChangePasswordView = new ChangePasswordView
                                                       {
                                                           DataContext =
                                                               changePasswordViewModel,
                                                           Owner = GetWindow(
                                                                   this)
                                                       };
        objChangePasswordView.ShowDialog();
        log.Debug("LaunchChangePasswordView" + Utility.FUNCTION_EXIT_LOG);
    }          
    /// <summary>
    /// 
    /// </summary>
    /// <param name="selectionScreenViewModel"></param>
    [MessengerMessageSink(MessengerMessages.LAUNCH_SELECTIONSCREEN_VIEW, ParameterType = typeof(SelectionScreenViewModel))]
    protected void LaunchSelectionScreen(SelectionScreenViewModel selectionScreenViewModel)
    {
        SelectionScreenView selectionScreenView =
            new SelectionScreenView
                {
                    DataContext =
                        selectionScreenViewModel,
                    Owner =
                        GetWindow(this)
                };
        selectionScreenView.ShowDialog();
    }

    # endregion

    # region Public Methods
    /// <summary>
    /// Exit Application
    /// </summary>
    /// <param name="obj"></param>
    [MessengerMessageSink(MessengerMessages.EXIT_APPLICATION,ParameterType=typeof(object))]
    public void ExitApplication(object obj)
    {
        this.Close();
    }
    #endregion

}
#endregion

Запуск Views работает отлично (как запуск с помощью нажатия кнопки), но он не работает, когда я пытаюсь запустить его при загрузке ExecutionView.

Я полностью застрял .... ничего не работает, я даже пытался использовать диспетчер, но все та же ошибка .......

Что я пытаюсь сделать здесь, это когда я запускаю свое приложение ExecutionView загружается в моем главном представлении ... в ExecutionViewModel Я выполняю некоторые проверки для запуска другого окна, но я чувствую, что мое представление выполнения еще не загружено так что это дает эту проблему .... но как ее решить ...

Спасибо за помощь .... заранее:)

Трассировка стека

at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
   at System.Windows.Window.ShowHelper(Object booleanBox)
   at System.Windows.Window.Show()
   at System.Windows.Window.ShowDialog()
   at MyMenuTool.ViewLayer.MainView.LaunchFileSelectView(PasswordViewModel passwordViewModel) in C:\AD\MyMenuTool\Code\Working\MyMenu_VS2010\MyMenu\MyMenu\MainView.xaml.cs:line 134
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...