1. 程式人生 > >只使用代碼創建窗口

只使用代碼創建窗口

sin ESS ace mes col vat his .com route

(1)添加一個類;(記住,不是添加一個窗體)

技術分享圖片

(2)添加命名空間,using system.windows;

(3)繼承window

(4)寫代碼

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

namespace 只使用代碼創建窗體
{
    class Windows1:Window
    {
        private Button button1;

        public Windows1()
        {
            Init();
        }
        void Init()
        {
            
this.Title = "只使用代碼創建的窗體"; this.Height = 600; this.Width = 600; this.Top = 100; button1 = new Button(); button1.Content = "請點擊我"; button1.Margin = new Thickness(10); button1.Padding = new Thickness(10); button1.Click
+= button1_click; this.AddChild(button1); } private void button1_click(object sensor,RoutedEventArgs e) { button1.Content = "已經被點擊"; } } }

效果

技術分享圖片

只使用代碼創建窗口