纯代码编写WPF程序

1.创建一个控制台应用程序

2. 首先要引入三个程序集如下。

//PresentationCore.dll
//PresentationFramework.dll
//System.Xaml

3. 编写window类

using System.Windows;
using System.Windows.Controls;

namespace ConsoleApplication21
{
    class MyWindow : Window
    {
        public Button button;

        public MyWindow()
        {
            Init();
        }

        private void Init()
        {
            this.Width = this.Height = 285;
            this.Left = this.Top = 100;
            this.Title = "Code-only Window";

            DockPanel panel = new DockPanel();
            this.AddChild(panel);

            button = new Button();
            panel.Children.Add(button);

            button.Content = "Please Click me.";
            button.Margin = new Thickness(30);

            button.Click += button_Click;
        }

        public void button_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("Thanks...");
        }
    }
}

4. 编写Application类,这里是应用程序的入口点

using System;
using System.Windows;

namespace ConsoleApplication21
{
    class Program : Application
    {
        [STAThread]
        static void Main(string[] args)
        {
            Program p = new Program();
            p.MainWindow = new MyWindow();
            p.MainWindow.ShowDialog();
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值