2009-06-05 58 views
0

在.NET中,我有一个名为Caption的类。我有另一个叫Gauge的课。在Gauge类中,我有一个定义为Caption的属性。触发另一个类中的事件

我想弄清楚如何做到以下几点: 当我的Caption类中的某个属性发生变化时,我该如何获取它以执行Gauge类中的子例程?我想我必须声明一个事件和AddHandlers来解雇它,但我想不出如何实现这一点。

回答

2

你会想看看在实现INotifyPropertyChanged界面,而这正是为此目的设计的 - 引发事件时的属性类实例更改。

this MSDN page上给出了一个很好的使用例子。

// This class implements a simple customer type 
// that implements the IPropertyChange interface. 
public class DemoCustomer : INotifyPropertyChanged 
{ 
    // These fields hold the values for the public properties. 
    private Guid idValue = Guid.NewGuid(); 
    private string customerName = String.Empty; 
    private string companyNameValue = String.Empty; 
    private string phoneNumberValue = String.Empty; 

    public event PropertyChangedEventHandler PropertyChanged; 

    private void NotifyPropertyChanged(String info) 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(info)); 
     } 
    } 

    // The constructor is private to enforce the factory pattern. 
    private DemoCustomer() 
    { 
     customerName = "no data"; 
     companyNameValue = "no data"; 
     phoneNumberValue = "no data"; 
    } 

    // This is the public factory method. 
    public static DemoCustomer CreateNewCustomer() 
    { 
     return new DemoCustomer(); 
    } 

    // This property represents an ID, suitable 
    // for use as a primary key in a database. 
    public Guid ID 
    { 
     get 
     { 
      return this.idValue; 
     } 
    } 

    public string CompanyName 
    { 
     get {return this.companyNameValue;} 

     set 
     { 
      if (value != this.companyNameValue) 
      { 
       this.companyNameValue = value; 
       NotifyPropertyChanged("CompanyName"); 
      } 
     } 
    } 
    public string PhoneNumber 
    { 
     get { return this.phoneNumberValue; } 

     set 
     { 
      if (value != this.phoneNumberValue) 
      { 
       this.phoneNumberValue = value; 
       NotifyPropertyChanged("PhoneNumber"); 
      } 
     } 
    } 
} 
2
public class Caption 
{ 
    private int myInt; 

    public event EventHandler MyIntChanged; 


    private void OnMyIntChanged() 
    { 
     var handler = this.MyIntChanged; 
     if (handler != null) 
     { 
      handler(this, EventArgs.Empty); 
     } 
    } 
    public int MyInt 
    { 
     get 
     { 
      return this.myInt; 
     } 
     set 
     { 

      if (this.myInt != value) 
      { 
       this.myInt = value; 
       this.OnMyIntChanged(); 
      } 
     } 
    } 
} 
现在

所以,在你有瓜葛类:

public class Guage 
{ 
    private Caption caption; 

    public Caption Caption 
    { 
     get 
     { 
      return this.caption; 
     } 
     set 
     { 
      if (this.caption!= value) 
      { 
       this.caption= value; 
       this.caption.MyIntChanged += new EventHandler(caption_MyIntChanged); 
      } 
     } 
    } 

    private void caption_MyIntChanged(object sender, EventArgs e) 
    { 
     //do what you gotta do 
    } 
} 
+1

其他方式 - 他希望Gauge类成为事件侦听器和Caption类来将其关闭。 – technophile 2009-06-05 20:31:25

+0

好吧,我想!这次我明白了。该死的,今天踢我的屁股......这是你想要的吗? – BFree 2009-06-05 20:37:13

相关问题