首页 > Windows开发 > 详细

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

时间:2020-05-18 00:31:54      阅读:79      评论:0      收藏:0      [点我收藏+]

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

 

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

原文:https://www.cnblogs.com/anyihen/p/12907679.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!