• 在VS中手工创建一个最简单的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了。

  • 相关阅读:
    mongdb
    网络编程
    分布式锁与事务
    mongodb
    Angular4
    思考
    kafka
    Spark总结
    你不知道的javaScript笔记(1)
    vue2.0 配置 选项 属性 方法 事件 ——速查
  • 原文地址:https://www.cnblogs.com/pemp/p/3593337.html
Copyright © 2020-2023  润新知