0

My WPF application calls upon a separate project to handle a login process BEFORE the Main Window in my application is shown. This creates a problem and causes "Application Shutdown" errors because the FIRST window in the application has closed. How can I handle the login process BEFORE my Main Window is shown? Every search I find comes up with references to Prism or MEF... which I cannot use.

Ryan D'Baisse
  • 837
  • 11
  • 29

3 Answers3

1

If you want to control everything from the very start of your application, you need to create your own main method and use this as "start object" (see project properties). More details can be found in another SO answer, but this is its essence:

[STAThread]
public static void Main(string[] args)
{
    // Do anything you like before running the main window.
    // ...

    // Proceed with usual application flow.
    var app = new MyApplication();
    var win = new MyWindow();
    app.Run(win);
}
Community
  • 1
  • 1
floele
  • 3,668
  • 4
  • 35
  • 51
0

To prevent the application shutdown error, you can change Application.ShutdownMode to OnExplicitShutdown. And explicitly call Application.Shutdown Method to close your application when needed.

Bolu
  • 8,696
  • 4
  • 38
  • 70
0

Have you tried adding code to the App.xaml.cs file? There are places you can place code in there that runs before the main window is opened. In addition to a constructor, there's the Startup event that you can assign a handler to in the App.xaml file:

<Application x:Class="CarSystem.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
         DispatcherUnhandledException="App_DispatcherUnhandledException" 
         Exit="Application_Exit" 
         Startup="Application_Startup" 
         StartupUri="MainWindow.xaml">

And, of course there's the Main method in the same file that you could throw code into, as well.

Tony Vitabile
  • 8,298
  • 15
  • 67
  • 123