2009-06-05 7 views
0

.NET에서 나는 Caption이라는 클래스가 있습니다. 게이지 (Gauge)라는 또 다른 클래스가 있습니다. Gauge 클래스에는 캡션으로 정의 된 속성이 있습니다.다른 클래스에서 이벤트 트리거

다음을 수행하는 방법을 알아 내려고합니다. 내 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"); 
      } 
     } 
    } 
} 
+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

좋아, 나는 생각한다 !! 나는 이번에 그것을 얻었다. 젠장, 오늘 내 엉덩이를 걷어차 ...이게 니가 원하는거야? – BFree