little cleanup

This commit is contained in:
Holger Börchers 2020-04-06 21:17:31 +02:00
parent b5744498dc
commit 365537102f
3 changed files with 30 additions and 49 deletions

View File

@ -1,11 +1,11 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using Prism.Mvvm;
namespace ModernWpfPlayground.MvvmStuff namespace ModernWpfPlayground.MvvmStuff
{ {
public abstract class BaseViewModel : INotifyPropertyChanged public abstract class BaseViewModel : BindableBase
{ {
private readonly Dictionary<string, object> _values = new Dictionary<string, object>(); private readonly Dictionary<string, object> _values = new Dictionary<string, object>();
@ -18,7 +18,7 @@ namespace ModernWpfPlayground.MvvmStuff
if (_values.TryGetValue(propertyName, out var obj) && Equals(value, obj)) return false; if (_values.TryGetValue(propertyName, out var obj) && Equals(value, obj)) return false;
_values[propertyName] = value!; _values[propertyName] = value!;
OnPropertyChanged(propertyName); RaisePropertyChanged(propertyName);
onChanged?.Invoke(value); onChanged?.Invoke(value);
return true; return true;
} }
@ -29,20 +29,12 @@ namespace ModernWpfPlayground.MvvmStuff
return Values.TryGetValue(propertyName, out var obj) ? (T) obj : defaultValue; 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() protected void ResetViewModel()
{ {
foreach (var key in Values.Keys) foreach (var key in Values.Keys)
{ {
_values.Remove(key); _values.Remove(key);
OnPropertyChanged(key); RaisePropertyChanged(key);
} }
} }
@ -53,7 +45,7 @@ namespace ModernWpfPlayground.MvvmStuff
foreach (var (key, value) in GetViewModelItems()) foreach (var (key, value) in GetViewModelItems())
{ {
_values[key] = value!; _values[key] = value!;
OnPropertyChanged(key); RaisePropertyChanged(key);
} }
} }
} }

View File

@ -2,7 +2,7 @@
using System.Reflection; using System.Reflection;
using System.Text.Json; using System.Text.Json;
namespace ModernWpfPlayground namespace ModernWpfPlayground.MvvmStuff
{ {
public static class PropertyInfoExtension public static class PropertyInfoExtension
{ {
@ -10,8 +10,7 @@ namespace ModernWpfPlayground
public static object? Convert(this PropertyInfo? propertyInfo, JsonElement value) public static object? Convert(this PropertyInfo? propertyInfo, JsonElement value)
{ {
if (propertyInfo == default) return default; if (propertyInfo == null) return default;
if (propertyInfo.PropertyType == typeof(double)) if (propertyInfo.PropertyType == typeof(double))
{ {

View File

@ -13,33 +13,10 @@ namespace ModernWpfPlayground
{ {
public class WindowViewModel : BaseViewModel public class WindowViewModel : BaseViewModel
{ {
private const string AppName = "TaBEA 3.0.0";
private readonly PropertyInfo[] _properties; private readonly PropertyInfo[] _properties;
private string? _path; private string? _path;
private string _title = AppName; 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() public WindowViewModel()
{ {
@ -55,11 +32,17 @@ namespace ModernWpfPlayground
_properties = GetType().GetProperties(); _properties = GetType().GetProperties();
} }
private async Task ShowDialogAsync() public string? Path
{ {
var dialog = new ContentDialogExample {Message = WelcomeMessage}; get => _path;
var result = await dialog.ShowAsync().ConfigureAwait(false); private set => SetProperty(ref _path, value,
WelcomeMessage = result.ToString(); () => Title = value != null ? $"{System.IO.Path.GetFileName(value)} - {AppName}" : AppName);
}
public string Title
{
get => _title;
set => SetProperty(ref _title, value);
} }
public bool BooleanValue public bool BooleanValue
@ -68,11 +51,6 @@ namespace ModernWpfPlayground
set => SetProperty(value, BooleanValue_OnChanged); set => SetProperty(value, BooleanValue_OnChanged);
} }
private void BooleanValue_OnChanged(bool obj)
{
VisibilityEnumTest = obj ? Visibility.Visible : Visibility.Collapsed;
}
public Visibility VisibilityEnumTest public Visibility VisibilityEnumTest
{ {
get => GetProperty<Visibility>(); get => GetProperty<Visibility>();
@ -107,6 +85,18 @@ namespace ModernWpfPlayground
public ICommand ResetViewModelCommand { get; } 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() private void SaveViewModel()
{ {
var contents = JsonSerializer.Serialize(Values); var contents = JsonSerializer.Serialize(Values);