mirror of
https://github.com/holgerb83/ModernWpfPlayground.git
synced 2025-06-30 01:40:51 +02:00
little cleanup
This commit is contained in:
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
43
MvvmStuff/PropertyInfoExtension.cs
Normal file
43
MvvmStuff/PropertyInfoExtension.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user