Es kann sein, dass ich dies tue das wirklich sehr harte Weise, aber ich habe festgestellt, dass mit dem Aufruf Vorrang/um beim Start der Bekämpfung von etwas unter der Annahme, sinnlos ist. Ich wollte wirklich jede Ausnahme, die während des Starts der Anwendung ausgelöst wurde, SOFORT zum äußersten Exception-Handler übergehen, aber selbst wenn eines ausgelöst wurde, instanziierte sich mein MVVM-Locator-Objekt automatisch selbst, da es als Ressource auf Anwendungsebene definiert wurde.
Das bedeutete, dass das Huhn vor dem Ei kam, aber nachdem das gleiche Ei bereits gebrochen war !!!
So war die Lösung:
1) Entfernen Sie MVVM Locator von App.xaml.
2) Ein neuer Application_Startup Ereignis
Fügen Sie diese Zeilen an der Spitze:
#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 Startup zum Application_Startup Ereignis in App.xaml z.B.
Startup="Application_Startup" <<<< this name is arbitrary but conventional AFAICT
4) In Applicaton_Startup, erstellen Sie die ViewModelLocator wie folgt aus:
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) Dann, unmittelbar nachdem die Ressource gefunden wurde, das Hauptfenster öffnen, aber nur, wenn der Locator wurde instanziiert erfolgreich
Schritt (4) wird sofort eine Ausnahme auslösen, wenn der Konstruktor auf dem Locator fehlschlägt, was die ganze Zeit für mich, REGRETTABLY.
Dann wird die Ausnahme von Schritt 4 wie folgt behandelt (dieses Beispiel eine RadMessageBox verwendet, aber frei zu beheben das Gefühl, dass:
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);
}
}
Die App noch die StartupUri zu öffnen versucht, diese versuchen - http. : //stackoverflow.com/a/6602102/2342414 – benshabatnoam