gpt4 book ai didi

c# - 检测 PropertyChangedEventHandler 何时更改

转载 作者:行者123 更新时间:2023-11-30 19:58:55 25 4
gpt4 key购买 nike

当我观察变量时,我的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com