Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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. </p> <p>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 <a href="http://msdn.microsoft.com/en-us/library/system.windows.application.unhandledexception%28v=vs.95%29.aspx" rel="noreferrer">Application.UnhandledException event</a>. 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. </p> <p><em>Disclaimer: All untested code, both in compilation and execution</em></p> <pre><code>public class MyView : UserControl { public MyView() { // Hardly best practice, but it'll work try { InitializeComponent(); } catch(Exception caught) { EventAggregatorService.Instance.GetEvent&lt;XamlExceptionEvent&gt;().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; } } </code></pre> <p>Taking it a step further, if you can create a base viewmodel like this</p> <pre><code>// 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 } } </code></pre> <p>Where derived view models are defined like this</p> <pre><code>// Derived viewmodel type public class DerivedViewModel : ErrorViewModel { public DerivedViewModel (IEventAggregator eventAggregator) : base(eventAggregator) { } } </code></pre> <p>You could apply the following method to your view as follows, by catching the exception and deferring error handling. </p> <pre><code>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); } } </code></pre> <p>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 <code>Initializecomponent()</code> method via reflection. </p> <p>Finally, the code for UnhandledException handling:</p> <pre><code>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&lt;MyUnhandledExceptionEvent&gt;().Publish(/* ... */); // Recover from the error e.Handled = true; return; } } </code></pre> <p>Just some ideas. I'd be interested to hear if there is a defacto solution to this as I've often encountered this problem!</p> <p>Best regards</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload