Added Model-View-ViewModel framework support.

This commit is contained in:
2025-05-21 12:06:16 -04:00
parent 7f7edc25da
commit d51b227ca6
3 changed files with 60 additions and 0 deletions

View 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);
}
}
}

View 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));
}
}

View 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);
}
}
}