2009-06-05 22 views
0

In .NET habe ich eine Klasse namens Caption. Ich habe eine andere Klasse namens Gauge. Innerhalb der Gauge-Klasse habe ich eine Eigenschaft definiert als eine Caption.Ein Ereignis aus einer anderen Klasse auslösen

Ich versuche herauszufinden, wie Sie das Folgende tun: Wenn eine bestimmte Eigenschaft in meiner Caption-Klasse geändert wird, wie bekomme ich es eine Subroutine in der Gauge-Klasse auszuführen? Ich denke, ich muss ein Ereignis und AddHandlers deklarieren, um es abzufeuern, aber ich kann mir nicht vorstellen, wie ich das erreichen soll.

Antwort

2

Sie werden bei der Umsetzung der INotifyPropertyChanged Schnittstelle aussehen wollen, die genau für den Zweck ausgelegt ist - ein Ereignis auslöst, wenn eine Eigenschaft eines Änderungen der Klasseninstanz

Ein gutes Anwendungsbeispiel findet sich unter 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

Gründe für Herabstufungen, bitte? – 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(); 
      } 
     } 
    } 
} 

So, jetzt in Ihrer guage Klasse:

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

Anders herum - er möchte, dass die Gauge-Klasse der Ereignis-Listener und die Caption-Klasse das Ereignis auslöst. – technophile

+0

OK, ich denke !! Ich habe es diesmal. Verdammt, kick mein Hintern heute ... Ist das was du willst? – BFree

Verwandte Themen