ADD : First Personal MVVM Toolkit

commands-19-09
Lou BRODA 1 year ago
parent b57e64fe61
commit e411da6a8d

@ -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

@ -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<TModel> : 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 { }
}

@ -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>(T member, T value, Action<T> action, [CallerMemberName] string propertyName = null)
{
if (EqualityComparer<T>.Default.Equals(member, value)) return;
action(value);
OnPropertyChanged(propertyName);
}
protected virtual void SetProperty<T>(ref T member, T value, [CallerMemberName] string propertyName = null)
{
if (EqualityComparer<T>.Default.Equals(member, value)) return;
member = value;
OnPropertyChanged(propertyName);
}
#endregion
}
}

@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

@ -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<T> : ICommand
{
#region Fields
private readonly Action<T> execute;
private readonly Func<T, bool> 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<T> execute, Func<T,bool> canExecute = null)
{
this.execute = execute;
this.canExecute = canExecute ?? (t => true);
}
#endregion
}
public class RelayCommand : RelayCommand<object>
{
#region Constructor
public RelayCommand(Action execute, Func<bool> canExecute = null) : base(new Action<object> (o => execute()), canExecute != null ? new Func<object, bool>(o => canExecute()) : null)
{
}
#endregion
}
}
Loading…
Cancel
Save