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
在Microsoft显示错误消息之前,在Word加载项中捕获C#WPF未处理的异常_Wpf_Exception Handling_Add In_Dispatcher_Unhandled Exception - Fatal编程技术网

在Microsoft显示错误消息之前,在Word加载项中捕获C#WPF未处理的异常

在Microsoft显示错误消息之前,在Word加载项中捕获C#WPF未处理的异常,wpf,exception-handling,add-in,dispatcher,unhandled-exception,Wpf,Exception Handling,Add In,Dispatcher,Unhandled Exception,我正在使用C#和WPF开发一个Microsoft Word插件 在我的一个窗口中,助手类在事件中引发异常。我希望异常冒泡到窗口级别,以便捕获它并向用户显示错误消息。因为它在helper类中的事件中,所以我不能在窗口代码中用try/catch块来捕获方法调用 Application.Current返回null,因此我无法使用应用程序调度程序 我可以使用Dispatcher.CurrentDispatcher.UnhandledException并向其添加DispatcherUnhandledExc

我正在使用C#和WPF开发一个Microsoft Word插件

在我的一个窗口中,助手类在事件中引发异常。我希望异常冒泡到窗口级别,以便捕获它并向用户显示错误消息。因为它在helper类中的事件中,所以我不能在窗口代码中用try/catch块来捕获方法调用

Application.Current返回null,因此我无法使用应用程序调度程序

我可以使用
Dispatcher.CurrentDispatcher.UnhandledException
并向其添加
DispatcherUnhandledExceptionEventHandler
。这是有效的,异常被捕获。但是,Microsoft显示

应用程序中发生未处理的异常

调用我的事件处理程序之前的错误消息


我试图以错误的方式解决此问题,还是有办法抑制Microsoft未处理的异常错误消息?

在Microsoft Word捕获异常并抛出未处理的异常消息之前,使用UnhandledExceptionFilter捕获异常

Dispatcher.CurrentDispatcher.UnhandledExceptionFilter += 
  new DispatcherUnhandledExceptionFilterEventHandler(Dispatcher_UnhandledExceptionFilter);

void Dispatcher_UnhandledExceptionFilter(object sender, DispatcherUnhandledExceptionFilterEventArgs e)
{
  e.RequestCatch = false;
  // Display error message or close the window.
}

确保将RequestCatch设置为false,以便Word不会处理异常。

WinForms加载项也可以这样做吗?(即无WPF)。