2010-09-15 12 views
5

Desidero che l'applicazione WPF venga avviata solo in determinate condizioni. Ho provato la seguente senza successo:Come impedire il caricamento di un'app WPF?

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

Ma l'app funziona normalmente anche se la condizione non è soddisfatta

risposta

13

Prova questa:

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

L'applicazione sta ancora cercando di aprire la StartupUri Prova questo - http. : //stackoverflow.com/a/6602102/2342414 – benshabatnoam

3

Un'altra soluzione

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> 

Codice dietro

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

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); 
     } 
    } 
Problemi correlati