2010-11-15 24 views

回答

1

在您的App.xaml.cs文件中,您可以通過編程方式更改要在啓動時顯示的Window.xaml文件。這是一個過分簡化的例子。

protected override void OnStartup(StartupEventArgs e) 
{ 
    base.OnStartup(e); 

    System.Windows.Window startupWindow = null; 

    if(useLargeMode) 
    { 
     startupWindow = new LargeMainWindow(); 
    } 
    else 
    { 
     startupWindow = new SmallMainWindow(); 
    } 
    window.Show(); 
} 

您也可以通過在App.xaml文件改變的StartupUri做到這一點,但是這顯然將是很難在運行時更改。

<Application x:Class="Main.App" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    StartupUri="MainWindow.xaml" /> <!-- change this --> 

我還沒有嘗試綁定到XAML中的應用程序聲明中的屬性,但VS 2010不會抱怨這一點。我擔心應用程序爲了正常工作而設置了datacontext。試試看,讓我們知道它是如何工作的。 :)

<Application x:Class="Main.App" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    StartupUri="{Binding StartupWindow}"> 
+0

我有MainWindow.xaml和MainSmallWindow.xaml。使用這種方法,我可以將它們鏈接到相同的xmal.cs文件而無需重複代碼? – williamtroup 2010-11-16 08:32:33

+0

不,但你可以讓它們都將它們的所有調用thunk轉移到實現所有邏輯的共享類,或者你可以從'Window'類派生一個父類,然後在共享父類中實現公共邏輯。 – 2010-11-16 16:59:06

相關問題