C# 应用程序中的WPF重定向\u DispatcherUnhandledException

C# 应用程序中的WPF重定向\u DispatcherUnhandledException,c#,wpf,C#,Wpf,我是WPF的新手,在将app.xaml.cs Dispatch Unhandled Exception事件重定向到新窗口时遇到了一些问题 基本上,我试图模仿web应用程序处理错误的方式 错误被捕获 通过Elmah记录错误 错误通过Elmah发送电子邮件 如果日志记录/电子邮件成功,则会将用户重定向到错误页面,否则将不处理错误 我呼叫的代码是- private void Application_DispatcherUnhandledException(object sender,

我是WPF的新手,在将app.xaml.cs Dispatch Unhandled Exception事件重定向到新窗口时遇到了一些问题

基本上,我试图模仿web应用程序处理错误的方式

  • 错误被捕获
  • 通过Elmah记录错误
  • 错误通过Elmah发送电子邮件
  • 如果日志记录/电子邮件成功,则会将用户重定向到错误页面,否则将不处理错误
  • 我呼叫的代码是-

    private void Application_DispatcherUnhandledException(object sender,
                               System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
    {
        ..... Processing Code
    
        NavigationService svc = NavigationService.GetNavigationService(this);
    
        svc.Navigate(new DIEM.WindowsApps.DIEMWindows.ErrorWindow());
    }
    
    我遇到的问题是,在调用NavigationService.GetNavigationService(this)时,“this”为null(可能是因为App.xaml.cs不是页面,而是从应用程序继承的)

    这有什么办法吗?我做错事情了吗


    提前感谢。

    使用与应用程序中其他地方相同的导航服务。如果它是
    AppWindow
    ,您可以将其另存为
    App
    类中的属性并访问它:

    public AppWindow()
    {
        ((App)Application.Current).AppWindow = this;
    }
    
    class App : Application
    {
        public AppWindow AppWindow { get; set; }
    
        private void Application_DispatcherUnhandledException(object sender,
                                   DispatcherUnhandledExceptionEventArgs e)
        {
            //..... Processing Code
    
            NavigationService svc = NavigationService.GetNavigationService(AppWindow);
    
            svc.Navigate(new DIEM.WindowsApps.DIEMWindows.ErrorWindow());
        }
    
    }
    

    尝试使用
    GetNavigationService(主窗口)
    刚刚尝试过,仍然返回null。您在应用程序中使用导航吗?(即,它是XBAP还是主窗口是导航窗口?)Erm,pass。我真的不知道,只是帮别人捡起来的。如何查找?应用程序或窗口中是否有
    页面
    s?你通常是如何从一个屏幕移动到另一个屏幕的?谢谢,我来试试这个