Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从全局X关闭按钮调用viewModel中定义的方法_C#_Wpf_Multithreading_Mvvm_Burn - Fatal编程技术网

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

C# 从全局X关闭按钮调用viewModel中定义的方法,c#,wpf,multithreading,mvvm,burn,C#,Wpf,Multithreading,Mvvm,Burn,在我基于MVVM的WPF应用程序的viewModel中,我有两个命令来处理两种不同状态下的两次取消单击 当没有正在进行的操作进度时-它将中继view.close()方法 当进度正在进行时-它将cancelWaiting=true中继到进度回调,然后相应地暂停进度,同时调出取消确认消息框,并根据是/否取消或继续 这两个命令都具有canExecute属性,这些属性根据这两种状态进行相应的解析 我创建了一个新方法(在同一个viewModel中),它封装了这两个命令。现在,我需要在单击全局“X”关闭按钮

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

  • 当没有正在进行的操作进度时-它将中继
    view.close()
    方法
  • 当进度正在进行时-它将
    cancelWaiting=true
    中继到进度回调,然后相应地暂停进度,同时调出取消确认消息框,并根据是/否取消或继续
  • 这两个命令都具有canExecute属性,这些属性根据这两种状态进行相应的解析

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

    Closing += (sender, e) => viewModel.CloseWindowCommand();
    
    这导致出现未处理的异常:

    窗口关闭时,无法将可见性设置为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)
    
    在System.Windows.Window.VerifyNotClosing()中
    在System.Windows.Window.InternalClose(布尔关闭,布尔忽略取消)
    在System.Windows.Window.Close()中
    在Myapp.ViewModel.RootViewModel.b_uu0()中
    在Myapp.RelayCommand.Execute处(对象参数)
    在Myapp.ViewModel.RootViewModel.CloseWindowCommand()中
    在Myapp.View.RootView.WindowClose(对象发送方,CancelEventArgs e)
    在System.Windows.Window.OnClosing处(CancelEventArgs e)
    在System.Windows.Window.WmClose()中
    在System.Windows.Window.WindowFilterMessage(IntPtr hwnd、Int32 msg、IntPtr wParam、IntPtr lParam、Boolean和handled)
    位于System.Windows.Interop.HwndSource.PublicHooksFilterMessage(IntPtr hwnd、Int32 msg、IntPtr wParam、IntPtr lParam、Boolean和handled)
    在MS.Win32.HwndWrapper.WndProc(IntPtr-hwnd、Int32-msg、IntPtr-wParam、IntPtr-lParam、Boolean和handled)
    在MS.Win32.HwndSubclass.DispatcherCallbackOperation(对象o)上
    位于System.Windows.Threading.ExceptionWrapper.InternalRealCall(委托回调、对象参数、Int32 numArgs)
    位于MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(对象源、委托方法、对象参数、Int32 numArgs、委托catchHandler)
    位于System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority优先级、TimeSpan超时、委托方法、对象参数、Int32 numArgs)
    位于MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd、Int32 msg、IntPtr wParam、IntPtr lParam)
    
    我做错了什么

    我已经看了一些相关的问题和帖子(,),但到目前为止我还没有找到问题所在


    欢迎使用任何指针。

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

    +1可能是在视图上单击关闭按钮,视图执行命令,该命令再次通知视图关闭。只需添加一个标志来检查vm是否没有自行关闭。。