澄清: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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
那么,我明白了什么?(或者相信它).
从:
public event PropertyChangedEventHandler PropertyChanged;
Run Code Online (Sandbox Code Playgroud)
1.- PropertyChanged是一种方法.当ProperyChanged事件触发时将执行的那个.
怀疑:但这种方法从未实施过......
从:
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
Run Code Online (Sandbox Code Playgroud)
2.- NotifyPropertyChanged是一种方法.由我们创建,可以有我们想要的任何名称.当属性被修改时,我们将启动此方法.
问题:此方法是否会触发ProperyChanged事件?
怀疑:对我来说,正如我在那里看到的那样,没有人发起这个事件,但我们创建的方法是在触发时启动.但是因为它没有触发,而是我们直接启动方法而不是它...
Mixture最终认为:NotifyPropertyChanged使用Hanlder抛出事件,以便被"上级实体"(示例代码中的绑定源)捕获,它接收修改后的属性以便能够更新它.然后,如果我想知道哪些元素/类可以知道这类事件,我该怎么办?
我认为最后一个是正确的,但是因为我不是专家而是我的想法,同时试图理解它并写下这个问题,我希望你纠正我.
非常感谢!
更新
非常感谢大家!然后,我可以用我想要的方法来约束事件吗?我试过了:
objetos[usados] = new ItemDB();
objetos[usados].PropertyChanged += mensaje();
Run Code Online (Sandbox Code Playgroud)
附:
public async void mensaje(string cadena)
{
var dlg = new ContentDialog(){
Title = "My App",
Content = cadena,
PrimaryButtonText = "Yes",
SecondaryButtonText = "No"
};
var result = await dlg.ShowAsync();
}
Run Code Online (Sandbox Code Playgroud)
但是VS说:
错误1 Ninguna sobrecarga对应''mensaje'重合符'System.ComponentModel.PropertyChangedEventHandler'regocado
翻译:
错误1对应于'mensaje'的任何一个重载都与'System.ComponentModel.PropertyChangedEventHandler'委托匹配
为什么它不起作用,因为我的事件是一个字符串的arg,并mensaje作为参数和字符串接收?
Kai*_*und 12
我建议您在C#中查找Events和Delegates以进一步阅读.
public event PropertyChangedEventHandler PropertyChanged;
Run Code Online (Sandbox Code Playgroud)
PropertyChanged是一个EventHandler,它是一个委托.其他代码可以在此处注册,并在调用委托时执行.
那么INotifyPropertyChanged会发生什么:
某些代码(可能是Xaml中的绑定)为PropertyChanged事件注册:
yourobject.PropertyChanged += MethodThatShouldBeCalledWhenThePropertyChanges;
Run Code Online (Sandbox Code Playgroud)
(这在某个地方最适合自动生成,因为它是从xaml发生的,但你也可以这样用手.)
在NotifyPropertyChanged方法中,事件委托被执行.它只是执行添加到事件的所有方法.
那么回答你的问题:
是的,NotifyPropertyChanged中的代码"触发"事件.它调用添加到事件的每个方法.
任何代码都可以注册事件.
截至您的更新:
我再次建议读代表.
您可以将委托视为方法接口.它定义了一个方法必须采用特定的参数类型来匹配委托.
PropertyChanged的类型为PropertyChangedEventHandler,它接受一个对象和一个PropertyChangedEventArgs参数.
所以这样的任何方法都是合适的:
void MethodName(
Object sender,
PropertyChangedEventArgs e
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4586 次 |
| 最近记录: |