[英]Why does this method return a boolean?
Why return bool from SetProperty?为什么从 SetProperty 返回 bool? It's not used.它没有被使用。
This is from boilerplate code coming out of a Xamarin.Forms template.这是来自 Xamarin.Forms 模板的样板代码。
It's not used in inherited classes either.它也没有在继承的类中使用。
public class BaseViewModel : INotifyPropertyChanged
{
private bool isBusy = false;
public bool IsBusy
{
get { return isBusy; }
set { SetProperty(ref isBusy, value); }
}
protected bool SetProperty<T>(ref T backingStore,
T value,
[CallerMemberName] string propertyName = "",
Action onChanged = null)
{
if (EqualityComparer<T>.Default.Equals (backingStore, value))
return false;
backingStore = value;
onChanged?.Invoke();
OnPropertyChanged(propertyName);
return true;
}
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
{
var changed = PropertyChanged;
if (changed == null)
return;
changed.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
The return value of this SetProperty
method indicates if it has effectively changed its underlying backing field.此SetProperty
方法的返回值指示它是否有效地更改了其基础支持字段。
The IsBusy
implementation doesn't use the return value of SetProperty
, but in a derived class, which can access this protected SetProperty method, its return value could be used to perform additional specialized tasks. IsBusy
实现不使用SetProperty
的返回值,但在派生的 class 中,可以访问此受保护的 SetProperty 方法,其返回值可用于执行其他专门任务。
By the way, I actually didn't expect the BaseViewModel
class to have an IsBusy
property with a public setter.顺便说一句,我实际上没想到BaseViewModel
class 有一个带有公共设置器的IsBusy
属性。 As it is now, the outside world can determine if a BaseViewModel
instance is busy or not.就像现在一样,外界可以确定BaseViewModel
实例是否忙。 As I see it, that's somewhat strange.在我看来,这有点奇怪。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.