启动视图时出错:“暂停调度程序处理时无法执行此操作”

时间:2011-11-04 08:09:43

标签: wpf mvvm dispatcher

当我尝试从其他View ViewsModel的构造函数启动View时,我得到错误:
在调度程序处理暂停时无法执行此操作

我正在启动的My View的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

启动视图非常完美(如通过单击按钮启动)但是当我在加载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

0 个答案:

没有答案