从全局X关闭按钮调用viewModel中定义的方法

时间:2013-05-22 08:12:51

标签: c# wpf multithreading mvvm burn

在我基于MVVM的WPF应用程序的viewModel中,我有两个命令来处理两种不同的取消点击,处于两种不同的状态。

  1. 当没有正在进行的操作进度时 - 它会转发view.close()方法。
  2. 当进度正在进行时 - 它会将cancelWaiting = true中继到进度回调,然后在拉出取消确认消息框时依次暂停进度,并取决于是/否取消或继续。
  3. 这两个命令都有canExecute属性,根据这两个状态相应地解析。

    我创建了一个新方法(在同一个viewModel中),它封装了这两个命令。现在,我需要在单击全局“X”关闭按钮时调用此方法。我尝试了以下方法:

    Closing += (sender, e) => viewModel.CloseWindowCommand();
    

    这导致了未处理的异常:

    在窗口关闭时,无法将Visibility设置为Visible或调用Show,ShowDialog,Close或WindowInteropHelper.EnsureHandle。

    调用堆栈为:

    at System.Windows.Window.VerifyNotClosing()
       at System.Windows.Window.InternalClose(Boolean shutdown, Boolean ignoreCancel)
       at System.Windows.Window.Close()
       at Myapp.ViewModel.RootViewModel.<get_CloseCommand>b__0()
       at Myapp.RelayCommand.Execute(Object parameter)
       at Myapp.ViewModel.RootViewModel.CloseWindowCommand()
       at Myapp.View.RootView.WindowClose(Object sender, CancelEventArgs e)
       at System.Windows.Window.OnClosing(CancelEventArgs e)
       at System.Windows.Window.WmClose()
       at System.Windows.Window.WindowFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at System.Windows.Interop.HwndSource.PublicHooksFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
       at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
       at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
       at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
       at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
    

    我做错了什么?

    我查看了一些相关的问题和帖子(herehere),但到目前为止我无法找到问题。

    任何指针都表示赞赏。

1 个答案:

答案 0 :(得分:2)

似乎你在即将关闭的窗口上调用Close()。显然,你不应该这样做。也许你可以用viewModel.CloseWindowCommand中的一些参数提示窗口已经关闭了。

相关问题