WPF学习笔记-纯代码实现WPF程序

1,分析WPF引用和继承的类

2,手写界面

3,手写启动程序

 

界面代码,cs类

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using System.Windows;  //三个引用
 7 using System.Windows.Controls;
 8 using System.Windows.Markup;
 9 
10 namespace WPFdemo2
11 {
12     public class Windows1 : Window   
13     {
14         private Button button1 = null;
15         public Windows1()
16         {
17             //模仿建立一个初始化界面方法
18             InitializeComponent();
19         }
20         private void InitializeComponent()
21         {
22             //设置窗体基本信息
23             this.Height = 100;
24             this.Width = 200;
25             this.Title = "Code WPF";
26 
27             //创建窗体面板
28             DockPanel panel = new DockPanel();
29             //创建面板内元素对象,比如Button
30             button1 = new Button();
31             button1.Height = 50;
32             button1.Width = 100;
33             button1.Content = "Pls Click";
34             //元素的事件
35             button1.Click += Button1_Click;
36 
37             ////添加容器,指定面板
38             //IAddChild container = panel;
39             ////面板里面加button元素
40             //container.AddChild(button1);
41             ////改变面板指向当前对象
42             //container = this;
43             ////当前对象在加子元素面板
44             //container.AddChild(panel);
45 
46             //或者  直接指定容器为当前对象
47             IAddChild container = this;
48             //容器加面板元素
49             container.AddChild(panel);
50             //面板加子元素
51             panel.Children.Add(button1);
52 
53         }
54 
55         private void Button1_Click(object sender, RoutedEventArgs e)
56         {
57             MessageBox.Show("Hello Code WPF");
58         }
59     }
60 }
View Code

启动程序代码,cs类

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using System.Windows;
 7 namespace WPFdemo2
 8 {
 9     //继承运行程序
10     public  class Program:Application
11     {
12         //需要单线程启动
13         [STAThread]
14         static void Main()
15         {
16             Program app = new Program();
17             app.MainWindow = new Windows1();
18             app.MainWindow.ShowDialog();
19         }
20     }
21 }
View Code

 

posted @ 2020-05-17 23:16  安以痕_陈  阅读(386)  评论(0编辑  收藏  举报