2010-09-15 17 views
5

Ich möchte, dass die WPF-Anwendung nur unter bestimmten Bedingungen startet. Ich habe versucht, die folgenden ohne Erfolg:Wie kann verhindert werden, dass eine WPF-App geladen wird?

public partial class App : Application 
{ 
    protected override void OnStartup(StartupEventArgs e) 
    { 
     if (ConditionIsMet) // pseudo-code 
     { 
      base.OnStartup(e); 
     } 
    } 
} 

Aber die App läuft normal weiter, auch wenn die Bedingung nicht

erfüllt ist

Antwort

13

Versuchen Sie folgendes:

protected override void OnStartup(StartupEventArgs e) 
{ 
    base.OnStartup(e); 
    if (MyCondition) 
    { 
     ShowSomeDialog("Hey, I Can't start because..."); 
     this.Shutdown(); 
    } 
} 
+2

Die App noch die StartupUri zu öffnen versucht, diese versuchen - http. : //stackoverflow.com/a/6602102/2342414 – benshabatnoam

3

Eine andere Lösung

Designer

<Application x:Class="SingleInstanceWPF.App" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Startup="Application_Startup"> 
</Application> 

-Code hinter

public partial class App : Application 
{ 
    private void Application_Startup(object sender, StartupEventArgs e) 
    { 
     if (ConditionIsMet) 
     { 
      var window = new MainWindow(); 
      window.Show(); 
     } 
     else 
     { 
      this.Shutdown(); 
     } 
    } 
} 
0

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); 
     } 
    } 
Verwandte Themen