1

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

  1. 当没有正在进行的操作进度时 - 它会中继该view.close()方法。
  2. 当进度正在进行时 - 它会将 a 中继cancelWaiting = true到进度回调,然后相应地暂停进度,同时拉出取消确认消息框,并根据是/否取消或继续。

这两个命令都具有 canExecute 属性,可以根据两种状态相应地解析。

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

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

这导致了一个未处理的异常:

无法在 Window 关闭时将 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)

我究竟做错了什么?

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

任何指针表示赞赏。

4

1 回答 1

2

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

于 2013-05-22T08:17:56.403 回答