检测PropertyChangedEventHandler何时发生变化
当我观察变量时,我的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));
}
}
所以说这一切,我现在可以看到什么行应该工作,但我不知道为什么它不工作。任何其他想法?感谢你目前的帮助。
您可以编写自己的事件访问:
private PropertyChangedEventHandler propertyChanged;
public event PropertyChangedEventHandler PropertyChanged {
add { propertyChanged += value; }
remove { propertyChanged -= value; }
}
然后,您可以设置断点。 请注意,这不是线程安全的。
为了线程安全,应该使用'Delegate.Combine'和'Delegate.Remove'。 – Zer0 2014-09-03 20:34:41
@ Zer0:错; '+ ='和' - ='编译为这些方法,并且两者同样容易受到竞争条件的影响。 – SLaks 2014-09-04 00:01:59
哎呦。我想说的是,当你对一个事件执行'+ ='或' - ='时,他应该像.NET一样使用'Delegate.Combine'和'Delegate.Remove'以及'Interlocked.CompareExchange'。这样他可以在不引入竞争条件的情况下将其用于调试。 – Zer0 2014-09-04 14:47:47
向我们展示了'WatchTaskAsync'方法的调用代码。 – pushpraj 2014-09-02 09:47:36
http://msdn.microsoft.com/en-us/magazine/dn605875.aspx 我使用他的确切代码,并试图模仿他做事情的方式来使事情工作。它适用于一个项目,但不适用于另一个项目。 – 2014-09-02 15:28:33
看起来你正在尝试创建异步绑定。看看是否http://stackoverflow.com/questions/24731335/async-loading-images-in-wpf-value-converter/24731602#24731602可以帮助你在这种情况下。 – pushpraj 2014-09-03 05:04:24