问题
I'm using C# and Caliburn.
Is there a global way to catch all exceptions in all view models?
What about when there is some kind of exception during import from a DI/IoC container?
I basically want to display a message box when this occurs.
The RescueAttribute looks to me like it would have to be put on every single view model class, and it doesn't seem like it works when you use AsyncActionAttribute at the same time.
回答1:
The method IConventionManager.ApplyActionCreationConventions(IAction action, IMethod targetMethod)
is invoked just after an action creation and can add filters to it.
You can provide a custom IConventionManager
overriding DefaultConventionManager.ApplyActionCreationConventions
and pushing in the action a rescue filter.
Note that in this case you have to provide a rescue method on every ViewModel anyway; to overcome this, you can build a new custom rescue filter (following the line of the original one) and dispatch the exception to your infrastructure.
回答2:
There is a really simple way to achieve this, just override the UnhandledException method in your Bootstrapper.
There is an example here: Global handling exception in WPF app with Caliburn.Micro
来源:https://stackoverflow.com/questions/3366817/caliburn-exception-handling-and-rescue