VS中模仿WPF模板創(chuàng)建最簡單的WPF程序
如果不用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文件?! ?/p>
第四步:同第二步,添加兩個(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" Title="MainWindow" Height="350" Width="525"> <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)容,希望對(duì)大家的學(xué)習(xí)有所幫助。
相關(guān)文章
C# datagrid非常規(guī)方法實(shí)現(xiàn)添加合并列
這篇文章主要介紹了C# datagrid非常規(guī)方法實(shí)現(xiàn)添加合并列,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-11-11Unity編輯器資源導(dǎo)入處理函數(shù)OnPostprocessTexture實(shí)例深入解析
這篇文章主要為大家介紹了Unity編輯器資源導(dǎo)入處理函數(shù)OnPostprocessTexture實(shí)例深入解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09將c#編寫的程序打包成應(yīng)用程序的實(shí)現(xiàn)步驟分享(安裝,卸載) 圖文
時(shí)常會(huì)寫用c#一些程序,但如何將他們和photoshop一樣的大型軟件打成一個(gè)壓縮包,以便于發(fā)布.2011-12-12C#中的delegate委托類型基本學(xué)習(xí)教程
這篇文章主要介紹了C#中的delegate委托類型基本學(xué)習(xí)教程,委托是C#語言所具有的一個(gè)重要特性,需要的朋友可以參考下2016-01-01