, , , / , , - . , - , IMMEDIATELY , , MVVM Locator , .
, , , .
, :
1) MVVM Locator App.xaml.
2) Application_Startup
:
// anything else to do on startup can go here and will fire after the base startup event of the application
// First make sure anything after this is handled
// Creates an instance of the class holding delegate methods that will handle unhandled exceptions.
CustomExceptionHandler eh = new CustomExceptionHandler();
AppDomain.CurrentDomain.UnhandledException +=
new UnhandledExceptionEventHandler(eh.OnAppDomainException);
// this ensures that any unhandled exceptions bubble up to a messagebox at least
Dispatcher.CurrentDispatcher.UnhandledException += new DispatcherUnhandledExceptionEventHandler(eh.OnDispatcherUnhandledException);
3) Application_Startup App.xaml
.
Startup="Application_Startup" <<<< this name is arbitrary but conventional AFAICT
4) Applicaton_Startup ViewModelLocator :
Resources.Add("Locator", new ViewModelLocator());
if (!(TryFindResource("Locator") == null))
throw new ResourceReferenceKeyNotFoundException("ViewModelLocator could not be created", "Locator");
5) Then, immediately after discovering the resource, open MainWindow, but only if the locator was successfully created
Uri uri = new Uri("pack:Views/MainWindow.xaml", UriKind.RelativeOrAbsolute);
Application.Current.StartupUri = uri;
Step (4) will immediately throw an exception if the constructor on the locator does not work, WHICH ASKS ALL THE TIME ME RESUMES.
Then, the exception from step 4 is handled as follows (RadMessageBox is used in this example, but feel free to fix it:
public void OnDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
try
{
var result = this.ShowExceptionDialog(e.Exception);
}
catch
{
RadMessageBox.Show("Fatal Dispatcher Error - the application will now halt.", Properties.Resources.CaptionSysErrMsgDlg,
MessageBoxButton.OK, MessageBoxImage.Stop, true);
}
finally
{
e.Handled = true;
}
}