Added Model-View-ViewModel framework support.
This commit is contained in:
17
osrs-toolbox/MVVM/ModelBase.cs
Normal file
17
osrs-toolbox/MVVM/ModelBase.cs
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
namespace osrs_toolbox
|
||||||
|
{
|
||||||
|
public abstract class ModelBase : PropertyChangedBase
|
||||||
|
{
|
||||||
|
public void SetProperty<T>(ref T storage, T value, string name)
|
||||||
|
{
|
||||||
|
if (storage == null)
|
||||||
|
throw new InvalidCastException("osrs_toolbox.ModelBase.UpdateProperty\nStorage cannot be null.");
|
||||||
|
if (value == null)
|
||||||
|
throw new InvalidCastException("osrs_toolbox.ModelBase.UpdateProperty\nValue cannot be null.");
|
||||||
|
if (storage.GetType() != value.GetType())
|
||||||
|
throw new InvalidCastException("osrs_toolbox.ModelBase.UpdateProperty\nVariable type mismatch between storage and value.");
|
||||||
|
storage = value;
|
||||||
|
OnPropertyChanged(name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
10
osrs-toolbox/MVVM/PropertyChangedBase.cs
Normal file
10
osrs-toolbox/MVVM/PropertyChangedBase.cs
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
using System.ComponentModel;
|
||||||
|
|
||||||
|
namespace osrs_toolbox
|
||||||
|
{
|
||||||
|
public abstract class PropertyChangedBase : INotifyPropertyChanged
|
||||||
|
{
|
||||||
|
public event PropertyChangedEventHandler PropertyChanged;
|
||||||
|
public void OnPropertyChanged(string name) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
|
||||||
|
}
|
||||||
|
}
|
33
osrs-toolbox/MVVM/RelayCommand.cs
Normal file
33
osrs-toolbox/MVVM/RelayCommand.cs
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
using System.Windows.Input;
|
||||||
|
|
||||||
|
namespace osrs_toolbox
|
||||||
|
{
|
||||||
|
public class RelayCommand : ICommand
|
||||||
|
{
|
||||||
|
private Action<object> execute;
|
||||||
|
private Func<object, bool>? canExecute;
|
||||||
|
|
||||||
|
|
||||||
|
public event EventHandler? CanExecuteChanged
|
||||||
|
{
|
||||||
|
add { CommandManager.RequerySuggested += value; }
|
||||||
|
remove { CommandManager.RequerySuggested -= value; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public RelayCommand(Action<object> execute, Func<object, bool>? canExecute = null)
|
||||||
|
{
|
||||||
|
this.execute = execute;
|
||||||
|
this.canExecute = canExecute;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool CanExecute(object? parameter)
|
||||||
|
{
|
||||||
|
return this.canExecute == null || this.canExecute(parameter);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Execute(object? parameter)
|
||||||
|
{
|
||||||
|
this.execute(parameter);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user