Può essere che sto facendo questo il modo davvero duro, ma ho trovato che la lotta con l'invocazione di precedenza/ordine all'avvio assumendo tutto è inutile. Desideravo davvero che qualsiasi eccezione sollevata durante l'avvio dell'applicazione IMMEDIATAMENTE aumentasse fino al gestore di eccezioni più esterno, ma anche quando ne è stato generato uno, il mio oggetto MVVM Locator si stava istanziando automaticamente perché è definito come una risorsa a livello di applicazione.
Ciò significava che il pollo arrivava prima dell'uovo ma dopo lo stesso uovo si era già rotto !!!
Quindi la soluzione era:
1) Rimuovere MVVM Locator da App.xaml.
2) Creazione di un evento Application_Startup
Aggiungere queste righe in alto:
#region Handlers For Unhandled Exceptions
// 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);
#endregion Handlers For Unhandled Exceptions
3) Tie avvio alla manifestazione Application_Startup in App.xaml esempio
Startup="Application_Startup" <<<< this name is arbitrary but conventional AFAICT
4) In Applicaton_Startup, creare il ViewModelLocator in questo modo:
Resources.Add("Locator", new ViewModelLocator());
//You can use FindResource and an exception will be thrown straightaway as I recall
if (!(TryFindResource("Locator") == null))
throw new ResourceReferenceKeyNotFoundException("ViewModelLocator could not be created", "Locator");
5) Poi, subito dopo la risorsa è stata trovata, aprire il MainWindow, ma solo se il Locator E 'stato istanziato con successo
Uri uri = new Uri("pack:Views/MainWindow.xaml", UriKind.RelativeOrAbsolute);
Application.Current.StartupUri = uri;
Il punto (4) genera un'eccezione immediatamente se il costruttore sul localizzatore non funziona QUANTO SUCCEDE TUTTO IL TEMPO PER ME, REGOLATAMENTE.
Poi, l'eccezione dal punto 4 viene gestita come segue (questo esempio viene utilizzato un RadMessageBox ma sentitevi liberi di fissare tale:
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;
// TERMINATE WITH AN ERROR CODE -1!
//Environment.Exit(-1);
}
}
L'applicazione sta ancora cercando di aprire la StartupUri Prova questo - http. : //stackoverflow.com/a/6602102/2342414 – benshabatnoam