PropertyBinding
namespace PropertyBinding
{
using System;
public class PropertyBinding
{
public string PropertyName { get; private set; }
private Action<object> OnChangeEvent;
private Func<object> OnGetValue;
public PropertyBinding(string bindName, Func<object> onGetValue) : this(bindName, onGetValue, true)
{
}
public PropertyBinding(string bindName, Func<object> onGetValue, bool addBinding = true)
{
PropertyName = bindName;
OnGetValue = onGetValue;
OnChangeEvent = null;
if (addBinding)
PropertyBindingMa.AddBinding(this);
}
public virtual void AddListener(Action<object> onChangeEvent)
{
this.OnChangeEvent += onChangeEvent;
onChangeEvent?.Invoke(GetValue());
}
public virtual void RemoveListener(Action<object> onChangeEvent)
{
if (null != this.OnChangeEvent)
this.OnChangeEvent -= onChangeEvent;
}
public virtual object GetValue()
{
if (null == OnGetValue)
return default(object);
return OnGetValue();
}
public virtual void NotfiyValueChange()
{
this.OnChangeEvent?.Invoke(GetValue());
}
}
public class PropertyBindingType<T> : PropertyBinding
{
public delegate void PropertyChangeDelegate(T value);
private PropertyChangeDelegate OnChangeEvent;
private Func<object, T> OnGetValue;
private object Data;
public PropertyBindingType(string bindName, Func<object, T> onGetValue, object data = null) : base(bindName, null, false)
{
OnGetValue = onGetValue;
Data = data;
PropertyBindingMa.AddBinding(this);
}
public void AddListenerT(PropertyChangeDelegate onChangeEvent)
{
this.OnChangeEvent += onChangeEvent;
onChangeEvent?.Invoke(GetValueT());
}
public void RemoveListenerT(PropertyChangeDelegate onChangeEvent)
{
if (null != this.OnChangeEvent)
this.OnChangeEvent -= onChangeEvent;
}
public override object GetValue()
{
return GetValueT();
}
public T GetValueT()
{
if (null == OnGetValue)
return default(T);
return OnGetValue(Data);
}
public override void NotfiyValueChange()
{
this.OnChangeEvent?.Invoke(GetValueT());
}
public static void AddListenerT(string name, PropertyChangeDelegate onChangeEvent)
{
var property = PropertyBindingMa.GetBinding<T>(name);
if (property == null)
{
throw new Exception($"{name} 属性不存在");
}
else
{
property.AddListenerT(onChangeEvent);
}
}
}
}
PropertyBindingMa
using System;
using System.Collections.Generic;
namespace PropertyBinding
{
public class PropertyBindingMa
{
private static Dictionary<string, PropertyBinding> propertyMap = new Dictionary<string, PropertyBinding>();
public static void AddBinding(PropertyBinding binding)
{
if (propertyMap.ContainsKey(binding.PropertyName))
{
throw new Exception("绑定属性已存在");
}
else
{
propertyMap.Add(binding.PropertyName, binding);
}
}
public static void RemoveBinding(string key)
{
propertyMap.Remove(key);
}
public static PropertyBinding GetBinding(string key)
{
propertyMap.TryGetValue(key, out var value);
return value;
}
public static PropertyBindingType<T> GetBinding<T>(string key)
{
if (propertyMap.TryGetValue(key, out var value))
{
return (PropertyBindingType<T>)value;
}
return null;
}
public static void Notfiy(string key)
{
if (propertyMap.TryGetValue(key, out var value))
value.NotfiyValueChange();
}
}
}
测试程序
using System;
namespace PropertyBinding
{
class SourceData
{
private int bingNum;
public int BingNum { get => bingNum; set {
bingNum = value;
PropertyBindingMa.Notfiy(nameof(bingNum));
}
}
}
class Program
{
static void Main(string[] args)
{
var sourceData = new SourceData();
var propertyName = "bingNum";
new PropertyBindingType<int>(propertyName, (data) => ((SourceData)data).BingNum, sourceData);
PropertyBindingMa.GetBinding<int>(propertyName).AddListenerT(OnBindNumListener);
Console.WriteLine("更改属性值为 10");
sourceData.BingNum = 10;
Console.ReadLine();
}
private static void OnBindNumListener(int value)
{
Console.WriteLine($"属性监听成功:{value}");
}
}
}
属性监听成功:0
更改属性值为 10
属性监听成功:10
原文地址 C# 属性值监听管理