diff --git a/MvvmStuff/BaseViewModel.cs b/MvvmStuff/BaseViewModel.cs index 26ae437..4d435ef 100644 --- a/MvvmStuff/BaseViewModel.cs +++ b/MvvmStuff/BaseViewModel.cs @@ -1,11 +1,11 @@ using System; using System.Collections.Generic; -using System.ComponentModel; using System.Runtime.CompilerServices; +using Prism.Mvvm; namespace ModernWpfPlayground.MvvmStuff { - public abstract class BaseViewModel : INotifyPropertyChanged + public abstract class BaseViewModel : BindableBase { private readonly Dictionary _values = new Dictionary(); @@ -18,7 +18,7 @@ namespace ModernWpfPlayground.MvvmStuff if (_values.TryGetValue(propertyName, out var obj) && Equals(value, obj)) return false; _values[propertyName] = value!; - OnPropertyChanged(propertyName); + RaisePropertyChanged(propertyName); onChanged?.Invoke(value); return true; } @@ -29,20 +29,12 @@ namespace ModernWpfPlayground.MvvmStuff return Values.TryGetValue(propertyName, out var obj) ? (T) obj : defaultValue; } - public event PropertyChangedEventHandler? PropertyChanged; - - protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null) - { - if (propertyName == null) throw new ArgumentNullException(nameof(propertyName)); - PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); - } - protected void ResetViewModel() { foreach (var key in Values.Keys) { _values.Remove(key); - OnPropertyChanged(key); + RaisePropertyChanged(key); } } @@ -53,7 +45,7 @@ namespace ModernWpfPlayground.MvvmStuff foreach (var (key, value) in GetViewModelItems()) { _values[key] = value!; - OnPropertyChanged(key); + RaisePropertyChanged(key); } } } diff --git a/PropertyInfoExtension.cs b/MvvmStuff/PropertyInfoExtension.cs similarity index 91% rename from PropertyInfoExtension.cs rename to MvvmStuff/PropertyInfoExtension.cs index a2aa669..6264ad9 100644 --- a/PropertyInfoExtension.cs +++ b/MvvmStuff/PropertyInfoExtension.cs @@ -2,7 +2,7 @@ using System.Reflection; using System.Text.Json; -namespace ModernWpfPlayground +namespace ModernWpfPlayground.MvvmStuff { public static class PropertyInfoExtension { @@ -10,8 +10,7 @@ namespace ModernWpfPlayground public static object? Convert(this PropertyInfo? propertyInfo, JsonElement value) { - if (propertyInfo == default) return default; - + if (propertyInfo == null) return default; if (propertyInfo.PropertyType == typeof(double)) { diff --git a/WindowViewModel.cs b/WindowViewModel.cs index bb60dc0..7a1ec47 100644 --- a/WindowViewModel.cs +++ b/WindowViewModel.cs @@ -13,33 +13,10 @@ namespace ModernWpfPlayground { public class WindowViewModel : BaseViewModel { + private const string AppName = "TaBEA 3.0.0"; private readonly PropertyInfo[] _properties; private string? _path; private string _title = AppName; - private const string AppName = "TaBEA 3.0.0"; - - public string? Path - { - get => _path; - private set - { - if (Equals(_path, value)) return; - _path = value; - OnPropertyChanged(); - Title = value != null ? $"{System.IO.Path.GetFileName(value)} - {AppName}" : AppName; - } - } - - public string Title - { - get => _title; - set - { - if (Equals(_title, value)) return; - _title = value; - OnPropertyChanged(); - } - } public WindowViewModel() { @@ -55,11 +32,17 @@ namespace ModernWpfPlayground _properties = GetType().GetProperties(); } - private async Task ShowDialogAsync() + public string? Path { - var dialog = new ContentDialogExample {Message = WelcomeMessage}; - var result = await dialog.ShowAsync().ConfigureAwait(false); - WelcomeMessage = result.ToString(); + get => _path; + private set => SetProperty(ref _path, value, + () => Title = value != null ? $"{System.IO.Path.GetFileName(value)} - {AppName}" : AppName); + } + + public string Title + { + get => _title; + set => SetProperty(ref _title, value); } public bool BooleanValue @@ -68,11 +51,6 @@ namespace ModernWpfPlayground set => SetProperty(value, BooleanValue_OnChanged); } - private void BooleanValue_OnChanged(bool obj) - { - VisibilityEnumTest = obj ? Visibility.Visible : Visibility.Collapsed; - } - public Visibility VisibilityEnumTest { get => GetProperty(); @@ -107,6 +85,18 @@ namespace ModernWpfPlayground public ICommand ResetViewModelCommand { get; } + private async Task ShowDialogAsync() + { + var dialog = new ContentDialogExample {Message = WelcomeMessage}; + var result = await dialog.ShowAsync().ConfigureAwait(false); + WelcomeMessage = result.ToString(); + } + + private void BooleanValue_OnChanged(bool obj) + { + VisibilityEnumTest = obj ? Visibility.Visible : Visibility.Collapsed; + } + private void SaveViewModel() { var contents = JsonSerializer.Serialize(Values);