0

我有一个带有公共事件 PropertyChanged 的​​抽象基类。我有一个由另一个子类组成的子类,我想将事件传播给所有者。但是当我尝试连接事件时,我什么也没得到。

public abstract class ViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChange(string propertyName)
    {
        if (this.PropertyChanged == null) return;

        this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

public class Customer : ViewModel
{
   public Address Address
    {
        get { return _Address; }
        set
        {
            _Address = value;
            OnPropertyChanged("Address");
        }
    }
    private Address _Address = new Address();

    public Customer()
    {
        // I get nothing here. But why?
        Address.PropertyChanged += (o, e) => Logger.Log("Just do something, please!");

        // What I want to do is get Customer propertychange to fire
        // Because currently Address changes are not detected.
    }
}

public class Address : ViewModel
{
    private string _addy = "";
    public string Addy
    {
        get { return _addy; }
        set 
        {
            _addy=value;
            Logger.Log("Testing that at least something works");
            // I have verified that this is getting called, firing the event.
            OnPropertyChange("Addy");
        }
    }
}

编辑:更新:

对于那些好奇的人(我想是你,Servy)。问题原来是地址在代码的另一部分被重置。所以实际上连接在构造函数中按预期发生,然后由于 Address 设置为不同的实例而立即丢失。

我很欣赏这些回应。它帮助我缩小了问题的范围(不是语法错误)。它还帮助我了解如何在未来发布更好的问题。通过在困难的情况下发布问题,我能够找到一个简单但难以捉摸的答案。谢谢你。你的贡献非常有帮助。

但是,我认为这个问题证明了具体问题,并且这个问题已经并将继续更新以满足对可编译性的任何担忧(上面的代码工作得很好)。关于代码两件事。1. 每次有人有疑虑时,我都会更新它。2. 我已经多次使用这个网站,并且受益于那些能够真正分辨出像这个只需要伪代码来演示的概念问题之间的区别的人。所以我不知道使用伪代码会引起这样的骚动。诚挚的道歉。以后我会更加小心。

我认为这个问题说明了具体问题,因为我的具体问题是我无法弄清楚为什么事件在触发时没有连接。Servy 缩小了问题范围(这不是语法错误),Wonko 完全解决了问题(逻辑有缺陷,因为 Address 被覆盖),但我愿意更多地了解什么适合堆栈溢出,什么不适合.

在这一点上,我很乐意关闭这个话题,因为这对某些人来说似乎是个问题。但我不知道如何关闭它。一旦我意识到这对某些人来说意义重大,我就尝试删除它,但你不能删除带有评论的主题:(

4

2 回答 2

2

稍作修改,以便您的代码编译一切对我来说都很好:

[STAThread]
        static void Main()
        {          
            Customer cust = new Customer();
            cust.Address.Address1 = "HAllo";
        }

        public abstract class ViewModel : INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;

            protected void OnPropertyChange(string propertyName)
            {
                if (PropertyChanged == null) return;

                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }   
        }

        public class Customer : ViewModel
        {
            public Address Address { get; set; } //this implements inpc but I don't show that here.
            public Customer()
            {
                Address = new Address();
                // I get nothing here. But why?
                Address.PropertyChanged += (o, e) => Console.WriteLine("Just do something, please!");

                // What I want to do is get Customer propertychange to fire
                // Because currently Address changes are not detected.
            }
        }

        public class Address : ViewModel
        {
            private string _addy = "";
            public string Address1
            {
                get { return _addy; }
                set
                {
                    _addy = value;
                    Console.WriteLine("Testing that at least something works");
                    // I have verified that this is getting called, firing the event.
                    OnPropertyChange("Addy");
                }
            }
        }

请接受我上面的建议,认真阅读有关调试的内容。

于 2013-10-10T15:09:00.970 回答
1

据我了解,您希望您的客户类在其 Address 触发事件时触发 OnPropertyChanged 事件。如果是这样,您需要将客户类中的订阅更改为以下内容:

public Customer : ViewModel
{
    public Address Address {get; set;} //this implements inpc but I don't show that here.
    public Customer()
    {
        // I get nothing here. But why?
        Address.PropertyChanged += (o, e) => OnPropertyChange(e.PropertyName);

        // What I want to do is get Customer propertychange to fire
        // Because currently Address changes are not detected.
    }
}

你基本上在做的是重播事件触发

于 2013-10-10T15:17:09.703 回答