4

I am trying to see if it is possible to handle exceptions at the viewmodel level in addition to the global level handing (Application.UnhandledException). Currently, if one of the viewmodels throws an uncaught error the application will hard crash. I would like to some how catch this at the module level, publish an event, and have this module removed from the region it is occupying instead of taking down the entire application.

Has anyone ever implemented something like this?
Should this type of architecture be avoided?

4

1 に答える 1

2

I've run into the same problem in my own work. Covering all of the following seems to work for us:

DispatcherUnhandledException += OnDispatcherUnhandledException;

TaskScheduler.UnobservedTaskException +=TaskScheduler_UnobservedTaskException; \\exceptions in tasks

AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

Also, for module & viewmodel level exceptions, we try/catch and then publish an event through the EventAggregator. In our shell, we subscribe to this event to display/log errors. Since the shell most likely houses all of the regions, you should be able to remove/hide these modules from view. I will state however that modules cannot be Unloaded. This may not be an optimal solution and we are still exploring better ways to do this.

Shell:

EventAggregator.GetEvent<RaisedExceptionEvent>().Subscribe(RaisedException);

ViewModel:

try
{
}
catch (Exception ex)
{
            EventAggregator.GetEvent<RaisedExceptionEvent>().Publish(new ExceptionManager(ex,
                                                                                          ExceptionMessageType.
                                                                                              Default, true));
}
于 2012-11-15T20:27:11.203 に答える