手机
当前位置:查字典教程网 >编程开发 >C#教程 >VS中模仿WPF模板创建最简单的WPF程序
VS中模仿WPF模板创建最简单的WPF程序
摘要:如果不用VS的WPF项目模板,如何手工创建一个WPF程序呢?我们来模仿WPF模板,创建一个最简单的WPF程序。第一步:文件——新建——项目—...

如果不用VS的WPF项目模板,如何手工创建一个WPF程序呢?我们来模仿WPF模板,创建一个最简单的WPF程序。

第一步:文件——新建——项目——空项目,创建一个空项目。

第二步:添加引用,PresentationFramework,PresentationCore,WindowsBase,System,System.Xaml,这几个是WPF的核心dll。

第三步:在项目上右键添加新建项,添加两个“xml文件”,分别命名为App.xaml和MainWindow.xaml。可以看出,xaml文件其实就是xml文件。

第四步:同第二步,添加两个代码文件,即空白c#代码文件,分别命名为App.xaml.cs和MainWindow.xaml.cs。可以看到,这两个文件自动变成第二步生成的两个文件的code-behind文件。

第五步:添加Xaml和C#代码:

App.xaml和MainWindow.xaml中删除自动生成的xml文件头,分别添加如下Xaml标记:

<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(); } } }

第六步:如果此时编译就会报错,提示没有找到Main函数入口,这个Main函数其实不用自己添加,系统会自动生成的。打开App.xaml的文件属性,将生成操作由Page改为ApplicationDefinition。

第七步:此时应该已经可以正常运行了,系统默认输出是控制台应用程序,可以打开项目属性页,将输出类型改为Windows应用程序。

至此,一个模仿VS的WPF项目模板的最简单的WPF程序就OK了。

以上就是本文的全部内容,希望对大家的学习有所帮助。

【VS中模仿WPF模板创建最简单的WPF程序】相关文章:

C#中is与As运算符号的使用详解

c#初学简单程序实例代码介绍

C#中哈希表(Hashtable)的介绍及简单用法

c# n个数排序实现代码

用 C# 编写一个停放在任务栏上的图标程序

用.NET创建Windows服务的方法第1/2页

C#打印出正等腰三角形实例代码

C#设置子窗体在主窗体中居中显示解决方案

C# 图片与二进制转换的简单实例

C# 一个WCF简单实例

精品推荐
分类导航