如果不用VS的WPF項(xiàng)目模板,如何手工創(chuàng)建一個(gè)WPF程序呢?我們來模仿WPF模板,創(chuàng)建一個(gè)最簡單的WPF程序。
第一步:文件——新建——項(xiàng)目——空項(xiàng)目,創(chuàng)建一個(gè)空項(xiàng)目。
第二步:添加引用,PresentationFramework,PresentationCore,WindowsBase,System,System.Xaml,這幾個(gè)是WPF的核心dll。
第三步:在項(xiàng)目上右鍵添加新建項(xiàng),添加兩個(gè)“xml文件”,分別命名為App.xaml和MainWindow.xaml。可以看出,xaml文件其實(shí)就是xml文件。
第四步:同第二步,添加兩個(gè)代碼文件,即空白c#代碼文件,分別命名為App.xaml.cs和MainWindow.xaml.cs。可以看到,這兩個(gè)文件自動(dòng)變成第二步生成的兩個(gè)文件的code-behind文件。
第五步:添加Xaml和C#代碼:
App.xaml和MainWindow.xaml中刪除自動(dòng)生成的xml文件頭,分別添加如下Xaml標(biāo)記:
<Application x:Class="WpfApp.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" StartupUri="MainWindow.xaml"> <Application.Resources> </Application.Resources></Application>
<Window x:Class="WpfApp.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" <Grid> </Grid></Window>
App.xaml.cs和MainWindow.xaml.cs中分別添加類似如下的代碼:
using System.Windows;namespace WpfApp{ /// <summary> /// App.xaml 的交互邏輯 /// </summary> public partial class App : Application { }} using System.Windows;namespace WpfApp{ /// <summary> /// MainWindow.xaml 的交互邏輯 /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } }} 第六步:如果此時(shí)編譯就會(huì)報(bào)錯(cuò),提示沒有找到Main函數(shù)入口,這個(gè)Main函數(shù)其實(shí)不用自己添加,系統(tǒng)會(huì)自動(dòng)生成的。打開App.xaml的文件屬性,將生成操作由Page改為ApplicationDefinition。
第七步:此時(shí)應(yīng)該已經(jīng)可以正常運(yùn)行了,系統(tǒng)默認(rèn)輸出是控制臺(tái)應(yīng)用程序,可以打開項(xiàng)目屬性頁,將輸出類型改為Windows應(yīng)用程序。
至此,一個(gè)模仿VS的WPF項(xiàng)目模板的最簡單的WPF程序就OK了。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助。



















