和事件(特别是INotifyPropertyChanged) [英] and events (INotifyPropertyChanged, specifically)

查看:184
本文介绍了和事件(特别是INotifyPropertyChanged)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个非常奇怪的问题,我不能似乎重现一个小例子。对不起,如果这个问题有点模糊。

I've run into a really strange problem I can't seem to reproduce with a small example. Sorry if this question is a little vague.

我有一个包含地址的人。两者都继承自实现INotifyPropertyChanged的BaseEntity。我希望Person类不仅在Address被设置时,而且当该Address自身发生变化时,我的Get / set in就像这样:

I have a Person which contains an Address. Both inherit from BaseEntity which implements INotifyPropertyChanged. I want the Person class to NotifyPropertyChanged("Address") not only when an Address is set, but also when that Address itself changes, so my get/set in Person looks like this:

class Person : BaseEntity
{
    private Address address;
    public Address Address
    {
        get { return address; }
        set
        {
            address = value;
            NotifyPropertyChanged("Address");

            // propagate changes in Address to changes in Person
            address.PropertyChanged += (s, e) => { NotifyPropertyChanged("Address"); };
        }
    }

    ...
}

这个工作已经很好了几个月了。

This has worked nicely for months.

我已将[Serializable]添加到Person,Address和BaseEntity(和[field:NonSerialized] to BaseEntity PropertyChanged),现在当我更改Address(somePerson.Address.Street =something new),该地址的PropertyChanged的invocationCount为0,它曾经是1,所以Person没有得到通知,本身不是fire NotifyPropertyChanged(Address);

I've added [Serializable] to Person, Address, and BaseEntity (and [field: NonSerialized] to BaseEntity's PropertyChanged), and now when I make a change to Address (somePerson.Address.Street = "something new") that address's PropertyChanged's invocationCount is 0 where it used to be 1, so Person doesn't get notified, and doesn't itself fire NotifyPropertyChanged("Address");

再次,如果我从Person中删除[Serializable],它可以工作,如果我将其添加回来,它不起作用。我没有实际上序列化任何东西,我刚刚添加了[Serializable]属性。

Again, if I remove [Serializable] from Person, it works, and if I add it back, it doesn't work. I'm not actually serializing anything yet, I've just added the [Serializable] attribute.

任何想法?

推荐答案

我的猜测(从评论中的讨论中略微刺激)是,您应用程序中的某个代码是检测 Serializable] 并决定序列化对象。例如,缓存可能是可能的候选者 - 可能是任何深克隆代码。

My guess (slightly spurred on from a discussion in the comments) is that some code somewhere in your application is detecting [Serializable] and deciding to serialize the object. For example, a cache would be a likely candidate - as might any "deep clone" code.

尝试实现 ISerializable (或只是添加序列化回调),并添加一个断点。如果您的断点点击,请加载调用堆栈窗口并导航备份堆栈,以查看序列化对象的原因,以及为什么。

Try implementing ISerializable (or just add a serialization callback), and add a break-point. If your break-point hits, load the call-stack window and navigate back up the stack to see what is serializing your object, and why.

这篇关于和事件(特别是INotifyPropertyChanged)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆