2013-06-12 58 views
11

我正在使用Visual Studio 2012 C#。我創建了一個帶有主窗口的WPF應用程序項目,併爲我的項目添加了一個登錄窗口。我想將啓動窗口更改爲我的登錄窗口,但似乎無法這樣做。更改啓動窗口

我去了屬性,但我看到的所有是Myproject.app - 它應該不顯示我的項目的形式?

反正我試圖運行從代碼窗口,以及像這樣:

Application.Run(new Login()); 

但是,這似乎並沒有工作。它給出了一個錯誤說:

錯誤1的對象引用是所必需的非靜態字段,方法或屬性「System.Windows.Application.Run(System.Windows.Window)」

回答

35

通過更改Application.StartupUri更改啓動窗口更新App.xaml

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

如果窗口包含子文件夾內,你需要使用相對路徑。例如。 '「子文件夾\ MainWindow.xaml」' – JDB

+0

謝謝!我現在得到了靜態錯誤,當我嘗試打開我的主窗口:Application.Run(new MainWindow()); – Albertus

+0

你還有'Application.Run(...)'在你的代碼中,或者你刪除了嗎? – dkozl

-1

使用Application.Current.Run相反Application.Run

0

要更改啓動窗口編程去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(); 
    } 
}