简体   繁体   English

INotifyPropertyChanged事件未调用事件处理程序

[英]INotifyPropertyChanged Event Not Calling Event Handler

I'm doing a bit of MVVM work in SL 4.0, and I've got an event handler that never gets called, despite the event firing, and being non-null. 我在SL 4.0中做了一些MVVM工作,并且我有一个事件处理程序,尽管事件触发并且是非null的,但从未被调用。

First, I attach the event handler. 首先,我附加事件处理程序。 I stepped through this, and notice that after adding the event handler, that PropertyChanged is no longer null, HOWEVER, it's invocationCount is still 0, and it's invocationList is still null. 我逐步完成了这一步,并注意到添加事件处理程序后,PropertyChanged不再为null,但是,它的invocationCount仍为0,并且invocationList仍为null。 That's not expected right? 那不是预期的吧?

node.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(OnMonitoredDataSourceNodePropertyChanged);

Next, I change a property, and call my OnPropertyChanged method, like so: 接下来,我更改一个属性,然后调用我的OnPropertyChanged方法,如下所示:

OnPropertyChanged("CheckState");

Which fires the method. 触发方法。 I step through that, and upon inspecting the PropertyChanged event, it is NOT NULL which is expected, but, it has an invocationCount of 3, and the invocationList has 4 objects, the last of which is NULL. 我逐步进行检查,检查PropertyChanged事件时,它不是期望的NOT NULL,但是它的invocationCount为3,invocationList有4个对象,最后一个为NULL。 That makes no sense to me, there should only be the 1 handler that was previously assigned. 这对我来说毫无意义,应该只有先前分配的1个处理程序。 Instead, its some other list, and one that does not include my original handler. 相反,它是其他一些列表,并且其中不包括我的原始处理程序。

public void OnPropertyChanged(string propertyName) {
        if (this.PropertyChanged != null) {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

So, PropertyChanged is fired, but my handler is never called. 因此,PropertyChanged被触发,但是从未调用过我的处理程序。 I've been beating my head against a wall for a couple of hours on this one, any help is appreciated. 我一直在这个墙上撞了几个小时,不胜感激。

There is nothing wrong with your code as it appears in the question. 问题中出现的代码没有错。 It would suggest that 1) your code is not as is represented here, or 2) the instance you add the listener to in the first part is not the instance you are firing OnPropertyChanged on. 这可能表明1)您的代码与此处未表示的代码相同,或者2)您在第一部分中添加侦听器的实例不是您要触发OnPropertyChanged的实例。 Number 2 is my bet. 2号是我的赌注。

In order to test this, you should debug your application, placing a breakpoint where you add a listener, and where you are firing OnPropertyChanged . 为了对此进行测试,您应该调试应用程序,在添加侦听器的位置以及触发OnPropertyChanged位置放置一个断点。

Within Visual Studio, when the first breakpoint is hit, make an object ID on the instance referenced by the node variable. 在Visual Studio中,当第一个断点被击中时,在由node变量引用的实例上创建一个对象ID Let your application continue execution. 让您的应用程序继续执行。

When the second breakpoint hits in OnPropertyChanged , examine the object ID on this . 当第二断点命中OnPropertyChanged中,检查对象ID this You'll find out whether or not the instances are the same. 您将发现实例是否相同。

In all things debugging, determine what your assumptions are. 在所有调试工作中,确定您的假设是什么。 Because that's where your bug lies. 因为那是您的错误所在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM