gpt4 book ai didi

silverlight - INotifyPropertyChanged 背后的概念是什么?

转载 作者:行者123 更新时间:2023-12-04 01:38:57 26 4
gpt4 key购买 nike

所有使用 MVVM 的 Silverlight 示例都使用名为 IPropertyChanged 的​​接口(interface)。它背后的概念是什么,为什么我们需要在设置一些值时引发事件?

例如:-

public class UserNPC:INotifyPropertyChanged
{
private string name;
public string Name {
get { return name; }
set { name = value; onPropertyChanged(this, "Name"); }
}
public int grade;
public int Grade {
get { return grade; }
set { grade = value; onPropertyChanged(this, "Grade"); }
}

// Declare the PropertyChanged event
public event PropertyChangedEventHandler PropertyChanged;

// OnPropertyChanged will raise the PropertyChanged event passing the
// source property that is being updated.
private void onPropertyChanged(object sender, string propertyName)
{
if (this.PropertyChanged != null)
{
PropertyChanged(sender, new PropertyChangedEventArgs(propertyName));
}
}
}

INotifyPropertyChanged 的​​确切目的是什么?

最佳答案

您有以下依赖项:

查看 → 绑定(bind) → 模型

现在,概念如下:

如果您的模型对象中的某些数据发生变化,您需要提出 PropertyChanged事件。为什么?因为 Binding 对象已经用数据对象的 PropertyChanged 注册了一个方法。事件。

因此,当您的 Model 对象发生变化时,您所要做的就是引发事件,然后您就完成了。

当您这样做时,Binding 对象会通过您的事件收到有关更改的通知。 Binding 对象反过来让 View 对象知道发生了什么事。然后,View 对象可以在必要时更新 UI。

代码示例

这里有一个可编译的例子。设置几个断点,按 F11 单步执行代码,看看幕后发生了什么。请注意,此示例具有以下依赖关系: View → 模型。我遗漏了 Binding 对象。

using System;
using System.ComponentModel;

namespace INotifyPropertyChangedDemo
{
class Program
{
static void Main(string[] args)
{
// Create 2 listeners.
View1 view1 = new View1();
View2 view2 = new View2();

// Create 1 data object.
Model model = new Model();

// Connect listener with data object.
model.PropertyChanged += new PropertyChangedEventHandler(view1.MyPropertyChangedEventHandler);
model.PropertyChanged += new PropertyChangedEventHandler(view2.MyPropertyChangedEventHandler);

// Let data object publish change notification.
model.FirstName = "new name";

// Check whether all listeners got notified.
// ... via console.
}

public class Model : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;

private string firstName;
public string FirstName
{
get { return firstName; }
set
{
if (firstName != value)
{
firstName = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("FirstName"));
}
}
}
}
}

public class View1
{
public void MyPropertyChangedEventHandler(object source, PropertyChangedEventArgs arg)
{
Console.WriteLine("Listener 1: Changed Property: {0}", arg.PropertyName);
string newValue = ((Model) source).FirstName;
Console.WriteLine("Listener 1: Changed Property Value: {0}", newValue);
}
}

public class View2
{
public void MyPropertyChangedEventHandler(object source, PropertyChangedEventArgs arg)
{
Console.WriteLine("Listener 2: Changed Property: {0}", arg.PropertyName);
string newValue = ((Model)source).FirstName;
Console.WriteLine("Listener 2: Changed Property Value: {0}", newValue);
}
}
}
}

关于silverlight - INotifyPropertyChanged 背后的概念是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3557922/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com