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.Input; namespace STranslate.Utils { /// /// 通知 /// public class BaseVM : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; protected void UpdateProperty(ref T properValue, T newValue, [CallerMemberName] string properName = "") { if (object.Equals(properValue, newValue)) return; properValue = newValue; NotifyPropertyChanged(properName); } public void NotifyPropertyChanged([CallerMemberName] string propertyName = null) { this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } } /// /// Command /// public class RelayCommand : ICommand { private readonly Predicate _canExecute; private readonly Action _execute; public RelayCommand(Predicate canExecute, Action execute) { this._canExecute = canExecute; this._execute = execute; } public event EventHandler CanExecuteChanged { add { CommandManager.RequerySuggested += value; } remove { CommandManager.RequerySuggested -= value; } } public bool CanExecute(object parameter) { return _canExecute(parameter); } public void Execute(object parameter) { _execute(parameter); } } /// /// 单例辅助类 /// public abstract class SingletonMode where T : class { public static T _Instance; public static T Instance() { Type type = typeof(T); lock (type) { if (SingletonMode._Instance == null) { SingletonMode._Instance = (Activator.CreateInstance(typeof(T), true) as T); } return SingletonMode._Instance; } } } }