I'm attempting to refactor some code in a MVVM architecture.
The Model has public values that are changed directly.
The UI listens for changes in these values.
Below is the event signaling code:
public string LoadFilename { get { return _loadFilename; } set { _loadFilename = value; OnPropertyChanged(); } }
public string SaveFilename { get { return _saveFilename; } set { _saveFilename = value; OnPropertyChanged(); } }
public string OneSafFilename { get { return _oneSafFilename; } set { _oneSafFilename = value; OnPropertyChanged(); } }
public bool IsSaveEnabled { get { return _isSaveEnabled; } set { _isSaveEnabled = value; OnPropertyChanged(); } }
public bool IsLoadEnabled { get { return _isLoadEnabled; } set { _isLoadEnabled = value; OnPropertyChanged(); } }
public bool IsLoadCheckpointEnabled { get { return _isLoadCheckpointEnabled; } set { _isLoadCheckpointEnabled = value; OnPropertyChanged(); } }
public bool IsCheckpointEnabled { get { return _isCheckpointEnabled; } set { _isCheckpointEnabled = value; OnPropertyChanged(); } }
public bool IsScenariosEnabled { get { return _isScenariosEnabled; } set { _isScenariosEnabled = value; OnPropertyChanged(); } }
Here is the OnPropertyChanged
function:
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
var handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
This seems like a lot of boilerplate for something that should be natural in MVVM.
I'd like to make it more concise, but I'm not sure where to start.
With views listening to the above properties, what should the getters and setters look like?