事件似乎没有正确连接

本文关键字:连接 事件 | 更新日期: 2023-09-27 18:34:57

我有一个抽象基类,其中包含一个公共事件属性已更改。我有一个由另一个子类组成的子类,我想将事件传播给所有者。但是当我试图连接事件时,我什么也得不到。

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");
        }
    }
}

编辑:更新:

对于那些好奇的人(我认为就是你,塞尔维(。问题原来是地址在代码的另一部分被重置。因此,实际上挂接在构造函数中按预期发生,然后立即丢失,因为 Address 设置为不同的实例。

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

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

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

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

事件似乎没有正确连接

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

[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");
                }
            }
        }

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

我了解,您希望您的客户类在其地址触发事件时触发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.
    }
}

你基本上在做的是重播事件火灾