From e411da6a8d77ac5246a68cc80047050bfaa96ca0 Mon Sep 17 00:00:00 2001 From: lobroda Date: Mon, 2 Oct 2023 19:36:50 +0100 Subject: [PATCH] ADD : First Personal MVVM Toolkit --- LivreLand.sln | 8 ++- PersonalMVVMToolkit/BaseViewModel.cs | 47 ++++++++++++++ PersonalMVVMToolkit/ObservableObject.cs | 37 +++++++++++ .../PersonalMVVMToolkit.csproj | 9 +++ PersonalMVVMToolkit/RelayCommand.cs | 65 +++++++++++++++++++ 5 files changed, 165 insertions(+), 1 deletion(-) create mode 100644 PersonalMVVMToolkit/BaseViewModel.cs create mode 100644 PersonalMVVMToolkit/ObservableObject.cs create mode 100644 PersonalMVVMToolkit/PersonalMVVMToolkit.csproj create mode 100644 PersonalMVVMToolkit/RelayCommand.cs diff --git a/LivreLand.sln b/LivreLand.sln index 9a68948..302c811 100644 --- a/LivreLand.sln +++ b/LivreLand.sln @@ -22,7 +22,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DtoAbstractLayer", "DtoAbst EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JsonReader", "JsonReader\JsonReader.csproj", "{62D56765-482A-4B7A-B77A-517B0AABF443}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Utils", "Utils\Utils.csproj", "{D74E6DD7-ED6A-400C-89EE-FA3BB50D924F}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Utils", "Utils\Utils.csproj", "{D74E6DD7-ED6A-400C-89EE-FA3BB50D924F}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PersonalMVVMToolkit", "PersonalMVVMToolkit\PersonalMVVMToolkit.csproj", "{DBB77CBE-CB7A-4277-B625-60DBD5749C12}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -68,6 +70,10 @@ Global {D74E6DD7-ED6A-400C-89EE-FA3BB50D924F}.Debug|Any CPU.Build.0 = Debug|Any CPU {D74E6DD7-ED6A-400C-89EE-FA3BB50D924F}.Release|Any CPU.ActiveCfg = Release|Any CPU {D74E6DD7-ED6A-400C-89EE-FA3BB50D924F}.Release|Any CPU.Build.0 = Release|Any CPU + {DBB77CBE-CB7A-4277-B625-60DBD5749C12}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {DBB77CBE-CB7A-4277-B625-60DBD5749C12}.Debug|Any CPU.Build.0 = Debug|Any CPU + {DBB77CBE-CB7A-4277-B625-60DBD5749C12}.Release|Any CPU.ActiveCfg = Release|Any CPU + {DBB77CBE-CB7A-4277-B625-60DBD5749C12}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/PersonalMVVMToolkit/BaseViewModel.cs b/PersonalMVVMToolkit/BaseViewModel.cs new file mode 100644 index 0000000..7c6aabc --- /dev/null +++ b/PersonalMVVMToolkit/BaseViewModel.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PersonalMVVMToolkit +{ + public class BaseViewModel : ObservableObject + { + + #region Fields + + private TModel model; + + #endregion + + #region Properties + + public TModel Model + { + get => model; + private set + { + SetProperty(ref model, value); + } + } + + #endregion + + #region Constructor + + public BaseViewModel(TModel model) + { + Model = model; + } + + public BaseViewModel() + { + Model = default(TModel); + } + + #endregion + } + + public class BaseViewModel : ObservableObject { } +} diff --git a/PersonalMVVMToolkit/ObservableObject.cs b/PersonalMVVMToolkit/ObservableObject.cs new file mode 100644 index 0000000..21b6af8 --- /dev/null +++ b/PersonalMVVMToolkit/ObservableObject.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Runtime.CompilerServices; +using System.Text; +using System.Threading.Tasks; + +namespace PersonalMVVMToolkit +{ + public class ObservableObject : INotifyPropertyChanged + { + + #region Properties + + public event PropertyChangedEventHandler? PropertyChanged; + + protected virtual void OnPropertyChanged([CallerMemberName] string PropertyName = null) + => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(PropertyName)); + + protected virtual void SetProperty(T member, T value, Action action, [CallerMemberName] string propertyName = null) + { + if (EqualityComparer.Default.Equals(member, value)) return; + action(value); + OnPropertyChanged(propertyName); + } + + protected virtual void SetProperty(ref T member, T value, [CallerMemberName] string propertyName = null) + { + if (EqualityComparer.Default.Equals(member, value)) return; + member = value; + OnPropertyChanged(propertyName); + } + + #endregion + } +} diff --git a/PersonalMVVMToolkit/PersonalMVVMToolkit.csproj b/PersonalMVVMToolkit/PersonalMVVMToolkit.csproj new file mode 100644 index 0000000..cfadb03 --- /dev/null +++ b/PersonalMVVMToolkit/PersonalMVVMToolkit.csproj @@ -0,0 +1,9 @@ + + + + net7.0 + enable + enable + + + diff --git a/PersonalMVVMToolkit/RelayCommand.cs b/PersonalMVVMToolkit/RelayCommand.cs new file mode 100644 index 0000000..548d045 --- /dev/null +++ b/PersonalMVVMToolkit/RelayCommand.cs @@ -0,0 +1,65 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Input; + +namespace PersonalMVVMToolkit +{ + public class RelayCommand : ICommand + { + + #region Fields + + private readonly Action execute; + + private readonly Func canExecute; + + #endregion + + #region Properties + + public event EventHandler? CanExecuteChanged; + + public bool CanExecute(object? parameter) + { + T param = (T)parameter; + return canExecute(param); + } + + public void Execute(object? parameter) + { + T param = (T)parameter; + execute(param); + } + + public void RefreshCommand() + => CanExecuteChanged?.Invoke(this, EventArgs.Empty); + + #endregion + + #region Constructor + + public RelayCommand(Action execute, Func canExecute = null) + { + this.execute = execute; + this.canExecute = canExecute ?? (t => true); + } + + #endregion + } + + public class RelayCommand : RelayCommand + { + + #region Constructor + + public RelayCommand(Action execute, Func canExecute = null) : base(new Action (o => execute()), canExecute != null ? new Func(o => canExecute()) : null) + { + + } + + #endregion + } +}