using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
public abstract class BindableBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private readonly SynchronizationContext _syncContext;
private readonly TaskScheduler _uiTaskScheduler;
public BindableBase()
{
_syncContext = WindowsFormsSynchronizationContext.Current;
_uiTaskScheduler = TaskScheduler.FromCurrentSynchronizationContext();
}
protected virtual bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
{
if (EqualityComparer<T>.Default.Equals(storage, value))
{
return false;
}
storage = value;
RaisePropertyChanged(propertyName);
return true;
}
protected virtual bool SetProperty<T>(ref T storage, T value, Action onChanged, [CallerMemberName] string propertyName = null)
{
if (EqualityComparer<T>.Default.Equals(storage, value))
{
return false;
}
storage = value;
onChanged?.Invoke();
RaisePropertyChanged(propertyName);
return true;
}
protected virtual void RaisePropertyChanged([CallerMemberName] string propertyName = null)
{
//PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(propertyName)));
OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
}
protected virtual void OnPropertyChanged(PropertyChangedEventArgs args)
{
//if (PropertyChanged != null)
//{
// _syncContext.Post(new SendOrPostCallback(_ =>
// {
// PropertyChanged(this, args);
// }), null);
//}
Task.Factory.StartNew(() =>
{
PropertyChanged(this, args);
}, CancellationToken.None, TaskCreationOptions.None, _uiTaskScheduler);
//this.PropertyChanged?.Invoke(this, args);
}
}
***************************************************使用方法**************************************************
直接继承基类,在属性Set中使用RaisePropertyChanged事件
标签:PropertyChanged,propertyName,绑定,storage,System,value,基类,using,Winform From: https://www.cnblogs.com/HomeSapiens/p/18281562