我有一个带有公共事件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被设置为不同的实例。
我很感激回应。它帮助我缩小了不是的问题(它没有语法错误)。它还帮助我了解如何在将来发布更好的问题。通过在困难的情况下发布问题,我能够找到一个简单但难以捉摸的答案。谢谢。您的贡献非常有用。
但是,我认为这个问题演示了具体的问题,并且问题是并且继续更新以满足任何关于可编译性的问题(上面的代码工作得很好)。关于代码两件事。每次有人担心时我都会更新它。 2.我已经多次使用过这个网站,并且受益于那些能够真正区分这样一个概念性问题的人,只需要伪代码来证明。所以我不知道使用伪代码会引起这样的骚动。真诚的歉意。我将来会更加小心。
我认为这个问题演示了具体的问题,因为我的具体问题是我无法弄清楚为什么事件没有连接虽然它正在解雇。问题被Servy缩小了(它不是语法错误)由Wonko完全解决(由于地址被覆盖,逻辑有缺陷),但我愿意更多地了解什么是适合堆栈溢出和什么不是&# 39;吨。
此时我很乐意关闭这个话题,因为对某些人来说这似乎是个问题。但我不知道如何关闭它。一旦我意识到它对某些人来说是如此重要,我试着删除它,但你不能删除带注释的主题:(
答案 0 :(得分: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");
}
}
}
请听我上面的建议,阅读有关debugging严重的内容。
答案 1 :(得分: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.
}
}
你基本上做的是重播事件火灾