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
public class MyView : UserControl
{
public MyView()
{
// Hardly best practice, but it'll work
try
{
InitializeComponent();
}
catch(Exception caught)
{
EventAggregatorService.Instance.GetEvent<XamlExceptionEvent>().Publish(/* ... */);
}
}
}
public class EventAggregatorService
{
public IEventAggregator Instance { get { return _instance; } }
// NOTE: Must be called once in your bootstrapper to set the EA instance
public static void SetEventAggregator(IEventAggregator instance)
{
_instance = instance;
}
}
Taking it a step further, if you can create a base viewmodel like this
// Base viewmodel type to handle errors
public abstract class ErrorViewModel
{
private IEventAggregator eventAggregator;
protected ErrorViewModel(IEventAggregator eventAggregator)
{
_eventAggregator = eventAggregator;
}
public void HandleInitializationException(object view, Exception caught)
{
// Publish event via _eventAggregator
}
}
Where derived view models are defined like this
// Derived viewmodel type
public class DerivedViewModel : ErrorViewModel
{
public DerivedViewModel (IEventAggregator eventAggregator) : base(eventAggregator)
{
}
}
You could apply the following method to your view as follows, by catching the exception and deferring error handling.
public class MyView : UserControl
{
private readonly Exception _ex;
public MyView()
{
try { InitializeComponent(); } catch (Exception caught) { _ex = caught; }
}
protected override OnDataContextChanged(object sender, EventArgs e)
{
if (_ex == null) return;
var vm = view.DataContext as ErrorViewModel;
if (vm != null)
{
vm.HandleInitializationException(view, caught);
return;
}
throw new Exception("Error occurred during View initialization", _ex);
}
}
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:
public partial class App : Application
{
public App()
{
this.UnhandledException += this.Application_UnhandledException;
InitializeComponent();
}
private void Application_UnhandledException(object sender,
ApplicationUnhandledExceptionEventArgs e)
{
if (e.ExceptionObject is FileNotFoundException)
{
// Inform the user
EventAggregatorService.Instance.GetEvent<MyUnhandledExceptionEvent>().Publish(/* ... */);
// Recover from the error
e.Handled = true;
return;
}
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With