2010-09-09 193 views
0

我剛剛發現了一個INotifyPropertyChange接口。我設法實現了這個接口,並且一切正常。不過,我想知道是否有可能攔截代碼這一事件,並觸發一個功能 比方說,我有一個函數如何攔截NotifyPropertyChange事件

DoStuff() 

,我wan't啓動此功能,每次property1,property2或property3變化。 當然,我可以把這個功能放在我的課上,但這不是一個好主意(我認爲)。

+0

請在問題中指定有其他屬性需要過濾掉,這會引發NotifyPropertyChanged事件 – 2010-09-09 21:04:50

回答

1

如果您的意思是將處理此事件的內部方法,您可以通過在類構造函數中註冊事件來完成。例如:

public class AnswerViewModel : IAnswerViewModel 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 

    private string content; 

    public AnswerViewModel() 
    { 
     PropertyChanged += (sender, args) => DoStuff(); 
    } 

    public string Content 
    { 
     get { return content; } 
     set 
     { 
      content = value; 
      PropertyChanged(this, new PropertyChangedEventArgs("Content")); 
     } 
    } 

    public void DoStuff() 
    { 
     // this method will be called whenever PropertyChanged event raised 
    } 
} 

如果攔截方法屬於其他類:

public class PropertiesInterceptor 
{ 
    private readonly AnswerViewModel viewModel; 

    private readonly List<string> propertiesToIntercept = 
     new List<string> { "property1", "property2", "property3" }; 

    public PropertiesInterceptor(AnswerViewModel viewModel) 
    { 
     this.viewModel = viewModel; 
     viewModel.PropertyChanged += OnPropertyChanged; 
    } 

    private void OnPropertyChanged(object sender, PropertyChangedEventArgs args) 
    { 
     if (propertiesToIntercept.Contains(args.PropertyName)) 
     { 
      DoStuff(); 
     } 
    } 

    private void DoStuff() 
    { 
     // Do something with viewModel 
    } 
} 
+0

您可能希望將事件更改爲PropertyChanged + =(sender,args)=> if(args.PropertyName = =「property1」|| args.PropertyName ==「property2」|| args.PropertyName ==「property3」)DoStuff();' – 2010-09-09 16:55:24

+0

嗯如果我想調用的函數在其他類中? – Berial 2010-09-09 16:58:37

+0

@Berial,添加示例用法來截取不同的類比提高'PropertyChanged' – Elisha 2010-09-09 17:05:19

0

你需要用它來取代現有的NotifyPropertyChanged事件處理程序,或者只是當NotifyPropertyChanged被稱爲被調用?

如果你指的是第二,你可以簡單地註冊事件處理

編輯

如果屬性參數等於Property1您可以添加一個事件處理程序被調用的NotifyPropertyChanged,檢查, Property2或Property3,然後纔將其轉發給您要調用的實際功能。

+0

當NotifyPropertyChanged被調用時,我需要調用函數 - 但只有當Property1,Property2或Property3被更改時 – Berial 2010-09-09 16:57:18

0

攔截PropertyChanged事件:

​​

0

你可以從RaisePropertyChanged()方法火法:

public int Property1 
{ 
    get { return this.property1; } 
    set 
    { 
     if (this.property1 != value) 
     { 
      this.property1 = value; 
      RaisePropertyChanged("Property1"); 
     } 
    } 
} 

private void RaisePropertyChanged(string propertyName) 
{ 
    if (PropertyChanged != null) 
    { 
     PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
    } 

    DoStuff(); // Call DoStuff here. 
} 
0

偷以利沙的答案來回答你的問題在Merlyn的回答

public class AnswerViewModel : IAnswerViewModel 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 

    private string property1; 
    private string property2; 
    private string propertyX; 

    public AnswerViewModel() 
    { 
     PropertyChanged += (sender, args) => 
     { 
      if(args.PropertyName == "Property1" || args.PropertyName == "Property2") 
       DoStuff(); 
     } 
    } 

    public string Property1 
    { 
     get { return content; } 
     set 
     { 
      property1 = value; 
      PropertyChanged(this, new PropertyChangedEventArgs("Property1")); 
     } 
    } 
    public string Property2 
    { 
     get { return content; } 
     set 
     { 
      property2 = value; 
      PropertyChanged(this, new PropertyChangedEventArgs("Property2")); 
     } 
    } 
    public string PropertyX 
    { 
     get { return content; } 
     set 
     { 
      propertyX = value; 
      PropertyChanged(this, new PropertyChangedEventArgs("PropertyX")); 
     } 
    } 

    public void DoStuff() 
    { 
     // this method will be called whenever PropertyChanged event raised from Property1 or Property2 
    } 
} 

如果類DoS凝灰岩在是你可以做的成員

private otherClass 
    public AnswerViewModel() 
    { 
     PropertyChanged += (sender, args) => 
     { 
      if(args.PropertyName == "Property1" || args.PropertyName == "Property2") 
       otherClass.DoStuff(); 
     } 
    } 

否則,你可以只讓其他類在自己的主代碼中註冊一個事件。