diff --git a/MVVM/ViewModels/BaseViewModel.ps1 b/MVVM/ViewModels/BaseViewModel.ps1 new file mode 100644 index 0000000..75edea3 --- /dev/null +++ b/MVVM/ViewModels/BaseViewModel.ps1 @@ -0,0 +1,36 @@ +Add-Type -AssemblyName PresentationFramework + +# INotifyPropertyChanged Basisklasse fuer alle ViewModels +Add-Type @" +using System; +using System.ComponentModel; +using System.Windows.Input; + +namespace FirefoxAddinManager { + + public class ObservableBase : INotifyPropertyChanged { + public event PropertyChangedEventHandler PropertyChanged; + protected void OnPropertyChanged(string name) { + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name)); + } + } + + public class RelayCommand : ICommand { + private readonly Action _execute; + private readonly Func _canExecute; + + public RelayCommand(Action execute, Func canExecute = null) { + _execute = execute ?? throw new ArgumentNullException(nameof(execute)); + _canExecute = canExecute; + } + + public event EventHandler CanExecuteChanged { + add { System.Windows.Input.CommandManager.RequerySuggested += value; } + remove { System.Windows.Input.CommandManager.RequerySuggested -= value; } + } + + public bool CanExecute(object parameter) => _canExecute == null || _canExecute(parameter); + public void Execute(object parameter) => _execute(parameter); + } +} +"@ -ReferencedAssemblies PresentationFramework, PresentationCore, WindowsBase