C# 有没有办法简化ViewModels中属性的定义?
我有很多类似这样的房产:C# 有没有办法简化ViewModels中属性的定义?,c#,xamarin,xamarin.forms,C#,Xamarin,Xamarin.forms,我有很多类似这样的房产: string _aBtnState; public string ABtnState { get => _aBtnState; set => SetProperty(ref _aBtnState, value); } 下面是我使用的SetProperty方法: public class ObservableObject : INotifyPropertyChanged { protected virtual bool SetProperty<
string _aBtnState;
public string ABtnState { get => _aBtnState; set => SetProperty(ref _aBtnState, value); }
下面是我使用的SetProperty方法:
public class ObservableObject : INotifyPropertyChanged
{
protected virtual 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;
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName]string propertyName = "") =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
或许
SetMyProperty(ABtnState);
并且仍然可以使其具有相同的功能 我认为没有比这更简单的方法了。我建议您在VisualStudio中创建一个代码段,以加快该过程
private $type$ $field$;
public $type$ $property$
{
get => this.$field$;
set => this.SetValue(ref this.$field$, value);
}
VSCode
VS Mac
VS视窗
我认为没有更简单的方法。我建议您在VisualStudio中创建一个代码段,以加快该过程
private $type$ $field$;
public $type$ $property$
{
get => this.$field$;
set => this.SetValue(ref this.$field$, value);
}
VSCode
VS Mac
VS视窗
这是一种欺骗,在编译时注入notify代码。可能对您有效,也可能无效。Fody将自动为您包含INPCyou@Jason-你能解释什么是Fody以及如何使用它吗?有这样一个问题:哪种类型的代码会作弊,并在编译时注入notify代码。可能对您有效,也可能无效。Fody将自动为您包含INPCyou@Jason-你能解释一下什么是福迪以及如何使用它吗?