新建一个WPF项目,将App.Xaml和MainWindow.xaml删除
创建一个Window1的类,并继承自Window。注意需要添加三个using引用 ```csharp using System.Windows; using System.Windows.Controls; using System.Windows.Markup;
namespace 项目6 { class Window1:Window { private Button button1;
public Window1(){InitializeComponeent();}private void InitializeComponeent(){//设置窗体大小this.Width = 600;this.Height = 450;this.Left = this.Top = 100;Title = "Code-Only Window";DockPanel panel=new DockPanel();//创建面板button1 = new Button();button1.Content = "Please Click me!";button1.Margin=new Thickness(30);button1.Click += Button1_Click;IAddChild container = panel;container.AddChild(button1);container = this;container.AddChild(panel);}private void Button1_Click(object sender, RoutedEventArgs e){button1.Content = "Thank you!";}}
}
31. 再新建个Program.cs类,作为启动程序:```csharpusing System;using System.Windows;namespace 项目6 {class Program :Application{[STAThread()]static void Main(){Program app=new Program();app.MainWindow=new Window1();app.MainWindow.ShowDialog();}}}
项目包含的类
运行后的效果

点击后的效果

