Change Startup Window
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();
}
}
Albertus
Updated on April 04, 2021Comments
-
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 almost 11 yearsIf your window is contained within a subfolder, you would need to use the relative path. E.g.
"subfolder\MainWindow.xaml"
-
Albertus almost 11 yearsThank you! I now get the static error when i try to open my main window : Application.Run(new MainWindow());
-
dkozl almost 11 yearsdo you still have
Application.Run(...)
in your code or you removed that? -
Albertus almost 11 yearsI removed Application.Run(...)
-
dkozl almost 11 yearsSo in
App.xaml
you have<Application ... StartupUri="Login.xaml">
, you removedApplication.Run(...)
and it's still complaining aboutMainWindow
?! -
Albertus almost 11 yearsI'm now trying Close(); var m = new MainWindow(); m.Show(); and variations thereof
-
Albertus almost 11 yearsI changed the order and now it is working : var m = new MainWindow(); m.Show(); Close();
-
Najeeb about 4 years@JDBstillremembersMonica, thanks for the added suggestion. Worked beautifully (y)
-
mafortis almost 3 years
Solution Explorer
-> Program.cs->Programdouble click
-> Application.Run(new Form1());Change Form1() to MY_NEW_Window_NAME()