We have several modules in our application. I have a project with a group of custom user controls which is used in the modules. Note that this user controls are not injected to the modules instead it is used as a user cotrol in the XAML inline.
Now the user controls can throw exceptions when it is loaded. I would like to handle the exceptions at the module level. The idea is I have an eventaggregator injected to the module constructor and upon catching the exception I can fire an error event.
For example the mainview of the module is as follows
XAML
<inf:DockBase
x:Class="MainView"
xmlns:inf="clr-namespace:Infrastructure"
xmlns:usercontrols="clr-namespace:UserControls;assembly=Myusercontrollib">
<Grid>
<!--some code here-->
<usercontrols:CustomListControl source={Binding myList}/>
</Grid>
</inf:DockBase>
Code behind
public MainView()
{
InitializeComponent();
}
public MainView(MainViewViewModel viewmodel, IEventAggregator eventAggregator)
:this()
{
_eventAggregator = eventAggregator;
}
Where can I catch excpetions from the user control in the module level??
Just shooting from the hip. Perhaps the simplest method to catch exceptions on initialization is to allow access to your event aggregator via a singleton and raise the event from the view.
Once your application has started up however, if you wish to handle unhandled exceptions on the UI thread without the application crashing, you can try the Application.UnhandledException event. This lets you handle an exception in Silverlight or WPF and process it, then either cancel or continue to allow the exception to throw you out of the application.
Disclaimer: All untested code, both in compilation and execution
Taking it a step further, if you can create a base viewmodel like this
Where derived view models are defined like this
You could apply the following method to your view as follows, by catching the exception and deferring error handling.
Ok its not neat and its not pretty, but again it’ll work. Taking it a step further you may be able to create a base view type to factor out initialization too, however if you are inheriting multiple different base types then this won’t help you. Alternatively a help class to defer initialization, calling the
Initializecomponent()method via reflection.Finally, the code for UnhandledException handling:
Just some ideas. I’d be interested to hear if there is a defacto solution to this as I’ve often encountered this problem!
Best regards