Set()和RaisePropertyChanged()之间的区别 [英] Difference between Set() and RaisePropertyChanged()

查看:1121
本文介绍了Set()和RaisePropertyChanged()之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读 http://msdn.microsoft.com/zh- us/magazine/jj651572.aspx 来学习mvvm light框架. 我下载了源代码Friend.cs.

I am reading this http://msdn.microsoft.com/en-us/magazine/jj651572.aspx to learn mvvm light framework. I download the source code Friend.cs.

我的问题是为什么不同属性的某些set方法实现方式不同.

My question is why some set method of different property are implemented differently.

例如,名字的设置者是为什么我需要_firstName的'ref'关键字.

For example, the setter for First name is, why I need 'ref' keyword for _firstName.

 Set(FirstNamePropertyName, ref _firstName, value);

DateOfBirthString的二传手是"

And the setter for DateOfBirthString is "

RaisePropertyChanged(() => DateOfBirth);

何时会评估linq表达式?

When will the linq expression will be evaluated?

namespace MyFriends.Model
{
    [SimpleSerialize]
    public class Friend : ObservableObject
    {
        /// <summary>
        /// The <see cref="FirstName" /> property's name.
        /// </summary>
        public const string FirstNamePropertyName = "FirstName";

        private string _firstName;

        /// <summary>
        /// Sets and gets the FirstName property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        [SimpleSerialize(FieldName = "first_name")]
        public string FirstName
        {
            get
            {
                return _firstName;
            }
            set
            {
                Set(FirstNamePropertyName, ref _firstName, value);
            }
        }

        /// <summary>
        /// The <see cref="LastName" /> property's name.
        /// </summary>
        public const string LastNamePropertyName = "LastName";

        private string _lastName;

        /// <summary>
        /// Sets and gets the LastName property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        [SimpleSerialize(FieldName = "last_name")]
        public string LastName
        {
            get
            {
                return _lastName;
            }
            set
            {
                Set(LastNamePropertyName, ref _lastName, value);
            }
        }

        /// <summary>
        /// The <see cref="DateOfBirth" /> property's name.
        /// </summary>
        public const string DateOfBirthPropertyName = "DateOfBirth";

        private string _dateOfBirthString;

        /// <summary>
        /// Sets and gets the DateOfBirth property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        [SimpleSerialize(FieldName = "birthday")]
        public string DateOfBirthString
        {
            get
            {
                return _dateOfBirthString;
            }
            set
            {
                _dateOfBirthString = value;
                RaisePropertyChanged(() => DateOfBirth);
            }
        }

        public DateTime DateOfBirth
        {
            get
            {
                if (string.IsNullOrEmpty(_dateOfBirthString))
                {
                    return DateTime.MinValue;
                }

                return DateTime.ParseExact(DateOfBirthString, "d", CultureInfo.InvariantCulture);
            }
            set
            {
                _dateOfBirthString = value.ToString("d", CultureInfo.InvariantCulture);
            }
        }

        private string _imageUrl;

        [SimpleSerialize(FieldName = "picture")]
        public string ImageUrl
        {
            get
            {
                return _imageUrl;
            }
            set
            {
                _imageUrl = value;
                RaisePropertyChanged(() => ImageUri);
            }
        }

        public Uri ImageUri
        {
            get
            {
                return new Uri(_imageUrl);
            }
        }
    }
}

推荐答案

这两种方法之间的区别在于,Set方法替换了_firstName字段的旧值,然后引发了PropertyChanged事件,而RaisePropertyChanged仅引发PropertyChanged事件.

The difference between those two methods is that the Set method replaces the old value of the _firstName field and then raises the PropertyChanged event, while the RaisePropertyChanged only raises the PropertyChanged event.

在大多数情况下,您都想使用Set方法,因为它通过将通常需要完成的所有操作仅用一种方法包装在属性的设置器中而有助于缩短属性声明:

You'll want to use the Set method in most cases, since it helps to shorten property declarations by wrapping all that typically needs to be done within a property's setter in just one method:

  1. 它更新传递的字段的值,并用value的内容覆盖它,然后
  2. 引发PropertyChanged事件,以将此更新通知Views.
  1. It updates the value of the passed field and overwrites it with the content of value, and then
  2. raises the PropertyChanged event to notify Views about this update.

需要通过引用传递字段(因此使用ref _firstName)的原因是,在Set方法中不需要字段的内容,而是实际上对字段本身进行了更新.

The reason the field needs to be passed by reference (thus using ref _firstName) is that not the field's content is needed within the Set method, but the field itself is actually updated.

当一个属性的更新确实也影响其他属性时,RaisePropertyChanged方法很有用.在这种情况下,这些属性的内容需要手动更新,然后可以调用RaisePropertyChanged方法来通知View哪些属性实际上已更改.

The RaisePropertyChanged method is useful when an update of one property does also affect additional properties. In this case, these properties' contents need to be updated manually, then the RaisePropertyChanged method can be called to inform Views about which properties have actually changed.

这篇关于Set()和RaisePropertyChanged()之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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