【发布时间】:2015-01-20 10:36:52
【问题描述】:
说明: 1.- 我不知道这是否有一个特定的名称或单词可以用英语或编程俚语来引用它,所以这可能是一个重复的帖子,因为我无法查看它。
2.- 我完全是新手,我从未使用过处理程序,所以这是问题的一部分。
我正在尝试了解 NotifyPropertyChanged 机制的工作原理。基于:INotifyPropertyChanged,重点举例。 (我看的是西班牙文,上面如果不自动改的话可以改成英文原版。
现在我要提取让我想知道的主要代码,并尝试分析它。希望你能告诉我哪里(如果存在)我错了,我不能理解。 让我们关注实现接口的类。
// This is a simple customer class that
// implements the IPropertyChange interface.
public class DemoCustomer : INotifyPropertyChanged
{
// These fields hold the values for the public properties.
private Guid idValue = Guid.NewGuid();
private string customerNameValue = String.Empty;
private string phoneNumberValue = String.Empty;
public event PropertyChangedEventHandler PropertyChanged;
// This method is called by the Set accessor of each property.
// The CallerMemberName attribute that is applied to the optional propertyName
// parameter causes the property name of the caller to be substituted as an argument.
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
// The constructor is private to enforce the factory pattern.
private DemoCustomer()
{
customerNameValue = "Customer";
phoneNumberValue = "(312)555-0100";
}
// This is the public factory method.
public static DemoCustomer CreateNewCustomer()
{
return new DemoCustomer();
}
// This property represents an ID, suitable
// for use as a primary key in a database.
public Guid ID
{
get
{
return this.idValue;
}
}
public string CustomerName
{
get
{
return this.customerNameValue;
}
set
{
if (value != this.customerNameValue)
{
this.customerNameValue = value;
NotifyPropertyChanged();
}
}
}
public string PhoneNumber
{
get
{
return this.phoneNumberValue;
}
set
{
if (value != this.phoneNumberValue)
{
this.phoneNumberValue = value;
NotifyPropertyChanged();
}
}
}
好吧,我明白什么? (或相信它)。
发件人:
public event PropertyChangedEventHandler PropertyChanged;
1.- PropertyChanged 是一种方法。 ProperyChanged 事件 触发时将执行的事件。
怀疑:但是这个方法从来没有实现过……
发件人:
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
2.- NotifyPropertyChanged 是一种方法。由我们创建,可以有我们想要的任何名称。当属性被修改时,我们会启动这个方法。
问题:此方法是否会触发 ProperyChanged 事件?
怀疑:对我来说,正如我在那里看到的那样,没有人启动此事件,但我们创建的方法是在触发时启动。但是由于它没有触发,我们直接启动方法而不是它......
Mixture 最终认为:NotifyPropertyChanged 使用 Hanlder 抛出事件,以便被“上级实体”(示例代码中的绑定源)捕获,后者接收修改后的属性以便可以对其进行更新。那么,如果我想知道哪些元素/类可以感知这类事件,我该怎么办?
我认为最后一个是正确的,但由于我不是专家,而是我在试图理解它和写这个问题时的想法,我希望你能纠正我。
非常感谢!
更新
非常感谢大家!那么,我可以用我想要的方法订阅事件吗?我试过了:
objetos[usados] = new ItemDB();
objetos[usados].PropertyChanged += mensaje();
与:
public async void mensaje(string cadena)
{
var dlg = new ContentDialog(){
Title = "My App",
Content = cadena,
PrimaryButtonText = "Yes",
SecondaryButtonText = "No"
};
var result = await dlg.ShowAsync();
}
然后VS说:
错误 1 Ninguna sobrecarga 对应“mensaje”与“System.ComponentModel.PropertyChangedEventHandler”委托相一致
翻译:
错误 1 没有一个与“mensaje”对应的重载与“System.ComponentModel.PropertyChangedEventHandler”委托匹配
为什么它不起作用,因为我的事件是用一个字符串 arg 给出的,而mensaje 接收作为参数和字符串?
【问题讨论】:
-
PropertyChanged是事件处理程序,而不是方法。它实际上存储了一个委托列表(函数,如果你愿意的话),并且调用(实际上是Invoke'ing)PropertyChanged将调用所有注册的委托。您可以通过以下方式注册代表:customer.PropertyChanged += OnCustomerChanged;。然后,当您从类内部调用PropertyChanged时,将调用OnCustomerChanged。 -
谢谢彼得!更新了一点!
-
PropertyChanged的类型为PropertyChangedEventHandler。如果查看它的定义,您会发现它是一个接受两个参数的委托:object(发送者)和PropertyChangedEventArgs(有关更改的属性的信息)。但是,您的mensaje方法需要一个string参数,因此它不匹配。 -
谢谢两位!然后我会阅读有关事件的信息。再次感谢彼得的解释^^
-
@Sinatr 为什么这么粗鲁?如果他不知道,那么他可以要求它。这样其他人就可以将他引向具体的主题。