WPF框架如何处理MVVM性能循环更新? [英] How WPF framework handles cyclic update of properties in MVVM?

查看:611
本文介绍了WPF框架如何处理MVVM性能循环更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们假设属性名绑定到文本框鉴于这样。

Lets assume property Name is bind to TextBox in view like this.

private string name
public string Name
{
   get {return name;}
   set {
       name=value;
       OnPropertyChanged("Name");
   }
}

查看

<TextBox Text="{Binding Name, Mode=TwoWay"/>

当我们更新文本框中的文本,它会调用的名称属性的setter从而提高其中的PropertyChanged想再次更新UI。我很好奇,怎么WPF避免更新的递归,提高事件。难道是考虑该事件的发送完成

When we update the text in text box, it will call the setter in Name property which in turn raise PropertyChanged which suppose to update UI again. I am curious how WPF avoid recursion of update and raise event. is it done by considering the sender of that event?

推荐答案

一个标准实现的属性应该是这样的:

A standard implementation of a property should look like this:

private string name;

public string Name
{
   get { return name; }

   set
   {
       if( name != value )
       {
           name = value;
           OnPropertyChanged("Name");
       }
   }
}

请注意额外的如果,以确保如果属性的值实际上改变的情况下,才会引发。

Note the additional if to make sure the event is only raised if the value of the property actually changed.

这篇关于WPF框架如何处理MVVM性能循环更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆