C# 更改启动窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/17067392/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change Startup Window
提问by Albertus
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.
我正在使用 Visual Studio 2012 C#。我创建了一个带有主窗口的 WPF 应用程序项目,并在我的项目中添加了一个登录窗口。我想将启动窗口更改为我的登录窗口,但似乎无法这样做。
I went to the properties but all I see there is Myproject.app - should it not display the forms of my project?
我去了属性,但我看到的只有 Myproject.app - 它不应该显示我的项目的形式吗?
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)'
错误 1 非静态字段、方法或属性“System.Windows.Application.Run(System.Windows.Window)”需要对象引用
采纳答案by dkozl
To change startup window update App.xamlby changing Application.StartupUri:
要App.xaml通过更改来更改启动窗口更新Application.StartupUri:
<Application ... StartupUri="MainWindow.xaml">
回答by Farhad Karbasian
use Application.Current.Run Instead of Application.Run
使用 Application.Current.Run 而不是 Application.Run
回答by Vishnu Babu
To change the startup window programmatically go to App.xamlremove 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
要以编程方式更改启动窗口,请App.xaml删除该行StartupUri="MainWindow.xaml"(这将删除默认的启动窗口配置),现在添加启动事件Startup="Application_Startup",在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();
   }
}

