little cleanup

This commit is contained in:
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.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<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;
_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);
}
}
}

View File

@ -0,0 +1,43 @@
using System;
using System.Reflection;
using System.Text.Json;
namespace ModernWpfPlayground.MvvmStuff
{
public static class PropertyInfoExtension
{
public static PropertyInfo? Find(this PropertyInfo[] infos, string key) => Array.Find(infos, x => x.Name == key);
public static object? Convert(this PropertyInfo? propertyInfo, JsonElement value)
{
if (propertyInfo == null) return default;
if (propertyInfo.PropertyType == typeof(double))
{
return value.GetDouble();
}
if (propertyInfo.PropertyType == typeof(bool))
{
return value.GetBoolean();
}
if (propertyInfo.PropertyType == typeof(int))
{
return value.GetInt32();
}
if (propertyInfo.PropertyType.IsEnum)
{
return Enum.ToObject(propertyInfo.PropertyType, value.GetInt32());
}
if (propertyInfo.PropertyType == typeof(string))
{
return value.GetString();
}
return default;
}
}
}