Deriving from BindableBase更简单的绑定通知 GIS

Visual Studio creates a BindableBase class in the Common folder of your projects. (Don’t confuse this class with the BindingBase class from which Binding derives.)

BindableBase 是像下面这样定义的

public abstract class BindableBase : INotifyPropertyChanged

{ public event PropertyChangedEventHandler PropertyChanged;

protected bool SetProperty<T>(ref T storage, T value, [CallerMemberName] String propertyName = null)
{ if (object.Equals(storage, value))

return false; storage = value; this.OnPropertyChanged(propertyName); return true; }

protected void OnPropertyChanged([CallerMemberName] string propertyName = null)

{ var eventHandler = this.PropertyChanged;

if (eventHandler != null)

{ eventHandler(this, new PropertyChangedEventArgs(propertyName)); } } }

BindableBase 类的使用方法:

using Windows.UI; using ColorScrollWithDataContext.Common; namespace ColorScrollWithDataContext {
 public class RgbViewModel : BindableBase 
{ double red, green, blue;
 Color color = Color.FromArgb(255, 0, 0, 0);
 public double Red
{ 
set
 { if (SetProperty<double>(ref red, value)) Calculate(); }
 get { return red; } } 
public double Green { set { if (SetProperty<double>(ref green, value)) Calculate(); } get { return green; } }
 public double Blue { set { if (SetProperty<double>(ref blue, value)) Calculate(); } get { return blue; } }
 public Color Color { set { if (SetProperty<Color>(ref color, value)) { this.Red = value.R; this.Green = value.G; this.Blue = value.B; } } get { return color; } } 
void Calculate() { this.Color = Color.FromArgb(255, (byte)this.Red, (byte)this.Green, (byte)this.Blue); }
 } 
}

使用方法 ,定义字段 double red,在 Red里面 属性set 调用

SetProperty<double>(refred, value),就可以了
SetProperty<double>(ref red, value)返回true 表示red 改变,返回false 没有改变

。NET 4.5 。C# 5.0 这样用
原文地址:https://www.cnblogs.com/gisbeginner/p/2670452.html