1

In a viewModel of my MVVM based WPF application, I have a two commands to handle two different Cancel clicks, at two different states.

  1. When there is no ongoing operation progress - it relays the view.close() method.
  2. When progress is ongoing - it relays a cancelWaiting = true to the progress callback, which then accordingly pauses the progress while it pulls up a cancel confirmation message box and depending on yes/no either cancels or continues.

Both these commands have canExecute properties which resolve accordingly as per the two states.

I created a new method (in the same viewModel) which encapsulates these two commands. Now, I need to call this method when the global "X" close button is clicked. I tried the following:

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

This resulted in an unhandled exception:

Cannot set Visibility to Visible or call Show, ShowDialog, Close, or WindowInteropHelper.EnsureHandle while a Window is closing.

with the call stack as:

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)

What am I doing wrong?

I have looked at a few related questions and posts (here, here) but I have been unable to locate the problem so far.

Any pointers are appreciated.

Community
  • 1
  • 1
Manas
  • 521
  • 8
  • 26

1 Answers1

2

It seems you call Close() on a window that is about to close. You shouldn't do that, obviously. Maybe you could hint with some parameter in viewModel.CloseWindowCommand that the window is closing already.

JeffRSon
  • 10,404
  • 4
  • 26
  • 51
  • +1 likely what goes on is close button clicked on view, view executes the command, the command again tells view to close. Just add a flag to check if the vm is not getting closed by itself.. – stijn May 22 '13 at 08:35