Mvvm Set()和RaisePropertyChanged()之间的差异
我读这篇文章是为了学习mvvm light框架。 我下载了Friend.cs的源代码 我的问题是为什么不同属性的集合方法的实现方式不同 例如,firstName的setter是,为什么我需要_firstName的'ref'关键字Mvvm Set()和RaisePropertyChanged()之间的差异,mvvm,prism,mvvm-light,Mvvm,Prism,Mvvm Light,我读这篇文章是为了学习mvvm light框架。 我下载了Friend.cs的源代码 我的问题是为什么不同属性的集合方法的实现方式不同 例如,firstName的setter是,为什么我需要_firstName的'ref'关键字 Set(FirstNamePropertyName, ref _firstName, value); DateOfBirthString的setter为“ 何时计算linq表达式 namespace MyFriends.Model { [SimpleSeri
Set(FirstNamePropertyName, ref _firstName, value);
DateOfBirthString的setter为“
何时计算linq表达式
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);
}
}
}
}
名称空间MyFriends.Model
{
[简单化]
公共类好友:observeObject
{
///
///该属性的名称。
///
public const string FirstNamePropertyName=“FirstName”;
私有字符串_firstName;
///
///设置并获取FirstName属性。
///更改该属性的值将引发PropertyChanged事件。
///
[简单序列化(FieldName=“first_name”)]
公共字符串名
{
得到
{
返回_firstName;
}
设置
{
Set(FirstNamePropertyName,ref\u firstName,value);
}
}
///
///该属性的名称。
///
public const字符串LastNamePropertyName=“LastName”;
私有字符串_lastName;
///
///设置并获取LastName属性。
///更改该属性的值将引发PropertyChanged事件。
///
[简单序列化(FieldName=“last_name”)]
公共字符串姓氏
{
得到
{
返回_lastName;
}
设置
{
Set(LastNamePropertyName,ref\u lastName,value);
}
}
///
///该属性的名称。
///
public const string DateOfBirthPropertyName=“DateOfBirth”;
私有字符串dateOfBirthString;
///
///设置并获取DateOfBirth属性。
///更改该属性的值将引发PropertyChanged事件。
///
[SimpleSerialize(FieldName=“生日”)]
公共字符串DateOfBirthString
{
得到
{
返回出生日期字符串;
}
设置
{
_dateOfBirthString=值;
RaisePropertyChanged(()=>出生日期);
}
}
公共日期时间出生日期
{
得到
{
if(string.IsNullOrEmpty(_dateOfBirthString))
{
return DateTime.MinValue;
}
返回DateTime.ParseExact(DateOfBirthString,“d”,CultureInfo.InvariantCulture);
}
设置
{
_dateOfBirthString=value.ToString(“d”,CultureInfo.InvariantCulture);
}
}
私有字符串_imageUrl;
[简单序列化(FieldName=“picture”)]
公共字符串ImageUrl
{
得到
{
返回_imageUrl;
}
设置
{
_imageUrl=值;
RaisePropertyChanged(()=>ImageUri);
}
}
公共Uri图像Uri
{
得到
{
返回新的Uri(_imageUrl);
}
}
}
}
这两种方法的区别在于Set
方法替换\u firstName
字段的旧值,然后引发PropertyChanged
事件,而RaisePropertyChanged
只引发PropertyChanged
事件
在大多数情况下,您都希望使用Set
方法,因为它通过将通常需要在属性的setter中执行的所有操作包装在一个方法中,有助于缩短属性声明:
值的内容覆盖它,然后
PropertyChanged
事件以通知视图有关此更新ref\u firstName
)的原因是Set
方法中不需要字段的内容,但字段本身实际上是更新的
当一个属性的更新也会影响其他属性时,
RaisePropertyChanged
方法非常有用。在这种情况下,需要手动更新这些属性的内容,然后可以调用RaisePropertyChanged
方法来通知视图哪些属性实际发生了更改。在这两种方法中,Set
方法替换\u firstName
字段的旧值,然后引发PropertyChanged
事件,而RaisePropertyChanged
只引发PropertyChanged
事件
在大多数情况下,您都希望使用Set
方法,因为它通过将通常需要在属性的setter中执行的所有操作包装在一个方法中,有助于缩短属性声明:
值的内容覆盖它,然后
PropertyChanged
事件以通知视图有关此更新ref\u firstName
)的原因是Set
方法中不需要字段的内容,但字段本身实际上是更新的
当一个属性的更新也会影响其他属性时,
RaisePropertyChanged
方法非常有用。在这种情况下,需要手动更新这些属性的内容,然后可以调用RaisePropertyChanged
方法来通知视图哪些属性实际发生了更改。一个实例I found该集合(参考属性,值)
当你不需要的时候不工作
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);
}
}
}
}
private bool _IsRunning;
public bool IsRunning
{
get => _IsRunning;
set
{
SetProperty(ref _IsRunning, value);
RaisePropertyChanged("IsComplete");
}
}
public bool IsComplete => !IsRunning;