2014-08-29 82 views
2

當我觀察變量時,我的PropertyChanged事件得到了正確設置,但在代碼中的某處它被重置爲null,我不知道如何確定發生了什麼。檢測PropertyChangedEventHandler何時發生變化

這裏已經來到代碼:

public event PropertyChangedEventHandler PropertyChanged; 

    //this is in the NotifyTaskCompletion class, as used from the blog 
// http://msdn.microsoft.com/en-us/magazine/dn605875.aspx 
    private async Task WatchTaskAsync(Task task) 
    { 
     try 
     { 
      await task; //After this task, PropertyChanged gets set to a non-null method("Void OnPropertyChanged()") 
     } 
     catch 
     { 
     } 
     //PropertyChanged, which was set to a value after the task was run, and still not null during IdentifyCompleted getting set, is now null here 
     var propertyChanged = PropertyChanged; 
     if (propertyChanged == null) 
      return; 
     //other code 
} 


    //My Watch variable of PropertyChanged is still good after this setter is run. 
    public NotifyTaskCompletion<GraphicCollection> IdentifyCompleted 
    { 
     get { return _IdentifyCompleted; } 
     set 
     { 
      _IdentifyCompleted = value; 
     // _IdentifyCompleted.PropertyChanged+= new PropertyChangedEventHandler(this, new  PropertyChangedEventArgs("IdentifyCompleted")); 
      // NotifyPropertyChanged(); 
     } 
    } 

我的主要問題是,我不能使用{集;獲取;}上的PropertyChanged試圖找出它是越來越設置爲null。所以我的主要問題是,除非任何人看到明顯錯誤的東西,否則我將如何去查找它被設置爲null的位置?感謝您的幫助。

編輯

按照過去的海報建議,我把我的代碼如下:

private PropertyChangedEventHandler _propertyChanged; 
    public event PropertyChangedEventHandler PropertyChanged 
    { 
     add { _propertyChanged += value; } 
     remove { _propertyChanged -= value; } 
    } 

這裏是問題。

//this is in my View Model. The ViewModel CONTAINS NotifyTaskCompletion<GraphicCollection> IdentifyCompleted which in turn implements INotifyPropertyChanged and has its own PropertyChanged that is not getting set 

    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "") 
    { 
     if (PropertyChanged != null) 
     { 
//This line sets the PopertyChanged in the view model AND in the NotifyTaskCompletion class somehow, but I don't know how it is setting it properly in the NotifyTaskCompletion class in my other project where this code works, When I step through this line in my code, it doesn't trigger 
//the add{} of the PropertyChanged in NotifyTaskCompletion, but it DOES in my other project... 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 

所以說這一切,我現在可以看到什麼行應該工作,但我不知道爲什麼它不工作。任何其他想法?感謝你目前的幫助。

+0

向我們展示了'WatchTaskAsync'方法的調用代碼。 – pushpraj 2014-09-02 09:47:36

+0

http://msdn.microsoft.com/en-us/magazine/dn605875.aspx 我使用他的確切代碼,並試圖模仿他做事情的方式來使事情工作。它適用於一個項目,但不適用於另一個項目。 – 2014-09-02 15:28:33

+0

看起來你正在嘗試創建異步綁定。看看是否http://stackoverflow.com/questions/24731335/async-loading-images-in-wpf-value-converter/24731602#24731602可以幫助你在這種情況下。 – pushpraj 2014-09-03 05:04:24

回答

7

您可以編寫自己的事件訪問:

private PropertyChangedEventHandler propertyChanged; 
public event PropertyChangedEventHandler PropertyChanged { 
    add { propertyChanged += value; } 
    remove { propertyChanged -= value; } 
} 

然後,您可以設置斷點。 請注意,這不是線程安全的。

+0

爲了線程安全,應該使用'Delegate.Combine'和'Delegate.Remove'。 – Zer0 2014-09-03 20:34:41

+0

@ Zer0:錯; '+ ='和' - ='編譯爲這些方法,並且兩者同樣容易受到競爭條件的影響。 – SLaks 2014-09-04 00:01:59

+0

哎呦。我想說的是,當你對一個事件執行'+ ='或' - ='時,他應該像.NET一樣使用'Delegate.Combine'和'Delegate.Remove'以及'Interlocked.CompareExchange'。這樣他可以在不引入競爭條件的情況下將其用於調試。 – Zer0 2014-09-04 14:47:47

相關問題