mirror of
https://github.com/holgerb83/ModernWpfPlayground.git
synced 2025-06-30 01:40:51 +02:00
added theme mode to view and object accessor for setting properties
This commit is contained in:
@ -1,13 +1,20 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.CompilerServices;
|
||||
using FastMember;
|
||||
using Prism.Mvvm;
|
||||
|
||||
namespace ModernWpfPlayground.MvvmStuff
|
||||
{
|
||||
public abstract class BaseViewModel : BindableBase
|
||||
{
|
||||
protected BaseViewModel()
|
||||
{
|
||||
ObjectAccessor = ObjectAccessor.Create(this);
|
||||
}
|
||||
|
||||
private readonly Dictionary<string, object> _values = new Dictionary<string, object>();
|
||||
protected readonly ObjectAccessor ObjectAccessor;
|
||||
|
||||
protected IReadOnlyDictionary<string, object> Values => _values;
|
||||
|
||||
@ -44,8 +51,7 @@ namespace ModernWpfPlayground.MvvmStuff
|
||||
{
|
||||
foreach (var (key, value) in GetViewModelItems())
|
||||
{
|
||||
_values[key] = value!;
|
||||
RaisePropertyChanged(key);
|
||||
ObjectAccessor[key] = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,38 +1,35 @@
|
||||
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)
|
||||
public static object? Convert(this JsonElement value, Type propertyType)
|
||||
{
|
||||
if (propertyInfo == null) return default;
|
||||
if (propertyType == null) return default;
|
||||
|
||||
if (propertyInfo.PropertyType == typeof(double))
|
||||
if (propertyType == typeof(double))
|
||||
{
|
||||
return value.GetDouble();
|
||||
}
|
||||
|
||||
if (propertyInfo.PropertyType == typeof(bool))
|
||||
if (propertyType == typeof(bool))
|
||||
{
|
||||
return value.GetBoolean();
|
||||
}
|
||||
|
||||
if (propertyInfo.PropertyType == typeof(int))
|
||||
if (propertyType == typeof(int))
|
||||
{
|
||||
return value.GetInt32();
|
||||
}
|
||||
|
||||
if (propertyInfo.PropertyType.IsEnum)
|
||||
if (propertyType.IsEnum)
|
||||
{
|
||||
return Enum.ToObject(propertyInfo.PropertyType, value.GetInt32());
|
||||
return Enum.ToObject(propertyType, value.GetInt32());
|
||||
}
|
||||
|
||||
if (propertyInfo.PropertyType == typeof(string))
|
||||
if (propertyType == typeof(string))
|
||||
{
|
||||
return value.GetString();
|
||||
}
|
||||
|
Reference in New Issue
Block a user