手寫MVVM

小乐哥發表於2024-05-05
internal class DelegateCommand : ICommand
 {
     public event EventHandler? CanExecuteChanged
     {
         add { CommandManager.RequerySuggested += value; }
         remove { CommandManager.RequerySuggested -= value; }
     }

     public DelegateCommand(Action<object?> executeAction, Func<object?, bool>? canExecuteFunc = null)
     {
         ExecuteAction = executeAction;
         CanExecuteFunc = canExecuteFunc;
     }

     public bool CanExecute(object? parameter)
     {
         return CanExecuteFunc == null || CanExecuteFunc(parameter);
     }

     public void Execute(object? parameter)
     {
         ExecuteAction?.Invoke(parameter);
     }

     public Action<object?>? ExecuteAction { get; set; }
     public Func<object?, bool>? CanExecuteFunc { get; set; }
 }
    internal class NotificationObject : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler? PropertyChanged;

        protected void NotifyPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

相關文章