1.5 在Xaml中創建Application
上面的例子是使用C#代碼來創建Application的類。實際上在WPF應用程序中更常用的是使用Xaml來創建Application類實例。上面提到在Visual Studio里創建WPF應用程序通常使用項目模板,現在來考察使用Visual Studio項目模板創建WPF應用程序的過程。在File菜單中選擇“New Project”,Visual Studio會顯示圖1-4所示的會話框,你可以選擇WPF Application(桌面應用程序)或WPF Browser Application(Silverlight應用程序)。
當選擇WPF Application項目模板時,Visual Studio自動創建4個文件:App.xaml、App.xaml.cs、Window1.xaml和Window1.xaml.cs。這4個文件移植了兩個類,App.xaml和App.xaml.cs創建的是App類,它從Application類中派生出來。Window1.xaml和Window1.xaml.cs創建的是Window1類,它從類Window中派生出來。讓我們來看一下App.xaml:
<Application x:Class=" Yingbao.Chapter1.WPFStartUp.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" StartupUri="Window1.xaml"> <Application.Resources> </Application.Resources> </Application>

圖1-4 Visual Studio中的項目模板
x:Class屬性把xaml所對應的后臺C#類聯系起來,在這里是Yingbao.Chapter1.WPFStartUp.App,其中App是類名,Yingbao.Chapter1.WPFStartup是命名空間。App.xaml在編譯后產生一個部分類,它位于所創建的項目目錄下的obj\Debug子目錄中,文件名為App.g.cs。這個文件中的內容如下:
public partial class App : System.Windows.Application { [System.Diagnostics.DebuggerNonUserCodeAttribute()] public void InitializeComponent() { #line 4 "..\..\App.xaml" this.StartupUri = new System.Uri("Window1.xaml", System.UriKind.Relative); #line default #line hidden } [System.STAThreadAttribute()] [System.Diagnostics.DebuggerNonUserCodeAttribute()] public static void Main() { Yingbao.Chapter1.WPFStartUp.App app = new Yingbao.Chapter1.WPFStartUp.App(); app.InitializeComponent(); app.Run(); } }
它在App類中加入了兩個方法,InitializeComponent和Main。Main方法和我們在前面直接創建HelloWPF類時所移植的Main方法類似,由于一個應用程序只能有一個入口點,所以在App.xaml.cs中不能再移植Main方法。在InitializeComponent()方法中,設置了StartupUri屬性:
this.StartupUri = new System.Uri("Window1.xaml", System.UriKind.Relative);
它是對App.xaml中的StartupUri="Window1.xaml"翻譯。
我們可以在App.cs中移植應用程序的邏輯,下面是App.cs中的App部分類:
namespace Yingbao.Chapter1.WPFStartUp { public partial class App : Application { } }
Visual Studio為我們產生一個框架。
與App類似,Visual Studio產生的Window1.xaml,也是一個部分類,Window1的另一部分類在C#中。
<Window x:Class="Yingbao.Chapter1.WPFStartUp.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="300" Width="300"> <Grid> </Grid> </Window>
其中的x:Class屬性也是把XAML和后臺的C#類聯系起來。
namespace Yingbao.Chapter1.WPFStartUp { public partial class Window1 : Window { public Window1() { InitializeComponent(); } } }
它只產生一個構造函數Window1,其中調用了InitializeComponent方法,InitializeComponent在哪里呢?在xaml編譯后產生的AppWin.g.cs文件中:
namespace Yingbao.Chapter1.WPFStartUp { public partial class AppWin : System.Windows.Window, System.Windows.Markup.IComponentConnector { private bool _contentLoaded; [System.Diagnostics.DebuggerNonUserCodeAttribute()] public void InitializeComponent() { if (_contentLoaded) { return; _contentLoaded = true; System.Uri resourceLocater = new System.Uri("/WPFStartUp;component/window1.xaml", System.UriKind.Relative); #line 1 "..\..\Window1.xaml" System.Windows.Application.LoadComponent(this, resourceLocater); #line default #line hidden } [System.Diagnostics.DebuggerNonUserCodeAttribute()] [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.Ed itorBrowsableState.Never)] [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Des ign", "CA1033:InterfaceMethodsShouldBeCallableByChildTypes")] void System.Windows.Markup.IComponentConnector.Connect(int connectionId, object target) { this._contentLoaded = true; } } }
本書以下的章節都是以Visual StudioWPF項目模板所產生的代碼為基礎,小結一下:
● xaml中的x:Class屬性把xaml和后臺C#類聯系起來。
● Visual Studio利用.NET 2.0引入的部分類(partial class)技術,把一個類分為兩部分表述:一個是xaml文件,一個是C#文件。編譯后,xaml文件會產生一個后綴為.g.cs的C#文件,是xaml所產生的部分類。
● Application中的StartupUri設置了應用程序啟動時的主窗口。