最近學習數據驅動UI,瞭解到INotifyPropertyChanged這個介面的用法,看了很多網上的文章,自己作了一個總結。 INotifyPropertyChanged這個介面其實非常簡單,只有一個PropertyChanged事件,如果類繼承了這個介面,就必須實現介面。用VS的提示,就是補充了 ...
最近學習數據驅動UI,瞭解到INotifyPropertyChanged這個介面的用法,看了很多網上的文章,自己作了一個總結。
INotifyPropertyChanged這個介面其實非常簡單,只有一個PropertyChanged事件,如果類繼承了這個介面,就必須實現介面。用VS的提示,就是補充了一句話:
public event PropertyChangedEventHandler PropertyChanged;
using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; namespace Demo001 { class Student:INotifyPropertyChanged { private string name; private int age; public string Name { get { return name; } set { if (this.name == value) { return; } this.name = value; Notify("Name"); } } public int Age { get { return age; } set { if (this.age == value) { return; } this.age = value; Notify("Age"); } } public event PropertyChangedEventHandler PropertyChanged; protected void Notify(string propertyName) { if (this.PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } } }
剩下的就是對事件PropertyChanged的操作,於是我想可不可以直接定義這個事件而不繼承介面INotifyPropertyChanged,結果發現也是可以的。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; namespace Demo001 { class Student { private string name; private int age; public string Name { get { return name; } set { if (this.name == value) { return; } this.name = value; Notify("Name"); } } public int Age { get { return age; } set { if (this.age == value) { return; } this.age = value; Notify("Age"); } } public event PropertyChangedEventHandler PropertyChanged; protected void Notify(string propertyName) { if (this.PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } } }
只不過這時候的PropertyChanged是自定義的事件了,我們可以隨意改變這個名字,比如pChanged,但是繼承INotifyPropertyChanged介面後,只能用PropertyChanged這個名字。
在Form中註冊事件,附代碼:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace Demo001 { public partial class Form1 : Form { Student stu = new Student(); public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { stu.PropertyChanged += changed; } private void button1_Click(object sender, EventArgs e) { stu.Name = textBox1.Text; stu.Age = int.Parse(textBox2.Text); } public void changed(object sender, PropertyChangedEventArgs e) { switch(e.PropertyName) { case "Name": Console.WriteLine("Name Changed"); break; case "Age": Console.WriteLine("Age Changed"); break; } } } }
namespace Demo001 { partial class Form1 { /// <summary> /// 必需的設計器變數。 /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// 清理所有正在使用的資源。 /// </summary> /// <param name="disposing">如果應釋放托管資源,為 true;否則為 false。</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 窗體設計器生成的代碼 /// <summary> /// 設計器支持所需的方法 - 不要修改 /// 使用代碼編輯器修改此方法的內容。 /// </summary> private void InitializeComponent() { this.textBox1 = new System.Windows.Forms.TextBox(); this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.textBox2 = new System.Windows.Forms.TextBox(); this.button1 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // textBox1 // this.textBox1.Location = new System.Drawing.Point(59, 33); this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(100, 21); this.textBox1.TabIndex = 0; // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(12, 38); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(29, 12); this.label1.TabIndex = 1; this.label1.Text = "姓名"; // // label2 // this.label2.AutoSize = true; this.label2.Location = new System.Drawing.Point(12, 80); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(29, 12); this.label2.TabIndex = 3; this.label2.Text = "年齡"; // // textBox2 // this.textBox2.Location = new System.Drawing.Point(59, 75); this.textBox2.Name = "textBox2"; this.textBox2.Size = new System.Drawing.Size(100, 21); this.textBox2.TabIndex = 2; // // button1 // this.button1.Location = new System.Drawing.Point(59, 129); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(75, 23); this.button1.TabIndex = 4; this.button1.Text = "設定"; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(195, 182); this.Controls.Add(this.button1); this.Controls.Add(this.label2); this.Controls.Add(this.textBox2); this.Controls.Add(this.label1); this.Controls.Add(this.textBox1); this.Name = "Form1"; this.Text = "Form1"; this.Load += new System.EventHandler(this.Form1_Load); this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.TextBox textBox2; private System.Windows.Forms.Button button1; } }
備忘:委托是特殊的類,事件是一種委托,所以事件註冊,應該是“附加”方法,而不是“等於”方法。
委托將參數傳給相應的方法,一個作用是(子窗體)傳遞參數,另一個作用是(主窗體)調用方法。
委托傳遞參數,可以用於窗體傳值,主視窗利用子窗體構造函數傳值給子窗體,子窗體將值傳給委托(=子窗體傳值給主窗體的方法,從而傳值給主窗體)。
委托調用方法,主窗體註冊方法,子窗體定義委托(事件),在子窗體給委托傳值的時候觸發主窗體調用方法,從而改變主窗體的一些UI變化。