c# - 检测 PropertyChangedEventHandler 何时更改

标签 c# wpf

当我观察变量时,我的 PropertyChanged 事件已正确设置,但在代码的某处它被重置为 null,我不知道如何找出发生这种情况的位置。

代码如下:

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 上使用 {set;get;} 来尝试确定它在何处被设置为 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));
        }
    }

综上所述,我现在可以看到哪条线应该工作,但我不知道为什么它不工作。还有其他想法吗?感谢到目前为止的帮助。

最佳答案

您可以编写自己的事件访问器:

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

然后您可以设置断点。 请注意,这不是线程安全的。

关于c# - 检测 PropertyChangedEventHandler 何时更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25572942/

相关文章:

c# - Response.cookies出现在request.cookies中

c# - 为什么 WebApi 帮助页面中不显示操作

wpf - 带有 WPF ViewModel 导航操作的 MVVM Light

c# - NLog 不包含过滤器似乎不起作用

c# - 使用动态生成的控件进行 Gridview 排序和分页

wpf - 为什么这个标签在绑定(bind)时看起来与静态值不同?

wpf - 如何确定 DataGrid 中选定单元格的值? (WPF)

c# - WPF:如何在可视化树中向上移动以找到单击的 3d 模型所在的 Model3DGroup?

c# - 处理MediaElement事件,例如MediaOpened,BufferingStarted和MVVM模式?

C# 将时间分成小时 block