Change Startup Window

38,656

Solution 1

To change startup window update App.xaml by changing Application.StartupUri:

<Application ... StartupUri="MainWindow.xaml">

Solution 2

To change the startup window programmatically go to App.xaml remove the line StartupUri="MainWindow.xaml" (This will remove the default startup window configuration), now add the startup event Startup="Application_Startup", in App.xaml.cs

private void Application_Startup(object sender, StartupEventArgs e)
{
  If(somecase)
   {
     MainWindow mainWindow = new MainWindow ();
     mainWindow.Show();
   }
  else
   {
     OtherWindow otherWindow= new OtherWindow();
     otherWindow.Show();
   }
}
Share:
38,656
Albertus
Author by

Albertus

Updated on April 04, 2021

Comments

  • Albertus
    Albertus about 3 years

    I am using Visual Studio 2012 C#. I have created a WPF application project with a main window and added a login window to my project. I want to change the startup window to be my login window but can't seem to do so.

    I went to the properties but all I see there is Myproject.app - should it not display the forms of my project?

    Anyway I have tried running the window from code as well like so :

    Application.Run(new Login());
    

    But that does not seem to work. It gives an error saying :

    Error 1 An object reference is required for the non-static field, method, or property 'System.Windows.Application.Run(System.Windows.Window)'

  • JDB
    JDB almost 11 years
    If your window is contained within a subfolder, you would need to use the relative path. E.g. "subfolder\MainWindow.xaml"
  • Albertus
    Albertus almost 11 years
    Thank you! I now get the static error when i try to open my main window : Application.Run(new MainWindow());
  • dkozl
    dkozl almost 11 years
    do you still have Application.Run(...) in your code or you removed that?
  • Albertus
    Albertus almost 11 years
    I removed Application.Run(...)
  • dkozl
    dkozl almost 11 years
    So in App.xaml you have <Application ... StartupUri="Login.xaml">, you removed Application.Run(...) and it's still complaining about MainWindow?!
  • Albertus
    Albertus almost 11 years
    I'm now trying Close(); var m = new MainWindow(); m.Show(); and variations thereof
  • Albertus
    Albertus almost 11 years
    I changed the order and now it is working : var m = new MainWindow(); m.Show(); Close();
  • Najeeb
    Najeeb about 4 years
    @JDBstillremembersMonica, thanks for the added suggestion. Worked beautifully (y)
  • mafortis
    mafortis almost 3 years
    Solution Explorer -> Program.cs->Program double click -> Application.Run(new Form1()); Change Form1() to MY_NEW_Window_NAME()