2009-06-05 9 views
0

.NETでは私はCaptionというクラスを持っています。ゲージと呼ばれる別のクラスがあります。 Gaugeクラス内には、キャプションとして定義されたプロパティがあります。別のクラス内からイベントをトリガーします

私は次のようにする方法を理解しようとしています: 私のCaptionクラスで特定のプロパティが変更されたら、Gaugeクラスのサブルーチンを実行するにはどうすればよいですか?私はイベントを宣言してAddHandlerを呼び出さなければならないと思っていますが、これをどのように達成するか考えることはできません。今では

答えて

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"); 
      } 
     } 
    } 
} 
+0

ダウン投票の理由はどうですか? – Noldorin

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

+0

OK、私は思っています!私は今回はそれを得た。やあ、今日私のお尻をキック...これはあなたが欲しいものですか? – BFree

関連する問題