创建一个类,构建窗体,
class window1 : Window//继承window { private Button Button1;//创建一个button类 public window1()//构造函数 { InitializeComponent(); } private void InitializeComponent()//初始化事件 { //设置窗体 this.Width = 500; this.Height = 400; this.Left = this.Top = 500; this.Title = "Code_Only_Windows"; //创建停靠面板 DockPanel panel = new DockPanel(); //创建按钮对象 Button1 = new Button(); Button1.Content = "Please click me"; Button1.Margin = new Thickness(50); Button1.Click += Button1_Click; IAddChild container = panel;//创建一个容器,是panel container.AddChild(Button1);//把button1放入容器panel中 container = this;//改变容器为当前对象, container.AddChild(panel);//将panel放入当前对象 } private void Button1_Click(object sender, RoutedEventArgs e)//参数2,包含路由与事件相关联的状态信息和事件数据 { Button1.Content = "Thank_You"; }
创建一个类去调用
class Program : Application { [STAThread()]//指定应用程序的COM线程模型是单线程单元(sta) static void Main() { Program app = new Program();//新建一个program类 app.MainWindow = new window1();// app.MainWindow.ShowDialog();//open a window } }