mirror of
https://github.com/holgerb83/ModernWpfPlayground.git
synced 2025-06-30 01:40:51 +02:00
switched to yaml as filee format
This commit is contained in:
@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using FastMember;
|
||||
using Prism.Mvvm;
|
||||
@ -21,7 +22,7 @@ namespace ModernWpfPlayground.MvvmStuff
|
||||
protected bool SetProperty<T>(T value, Action<T>? onChanged = null,
|
||||
[CallerMemberName] string? propertyName = null)
|
||||
{
|
||||
if(propertyName == null) throw new ArgumentNullException(nameof(propertyName));
|
||||
if (propertyName == null) throw new ArgumentNullException(nameof(propertyName));
|
||||
if (_values.TryGetValue(propertyName, out var obj) && Equals(value, obj)) return false;
|
||||
|
||||
_values[propertyName] = value!;
|
||||
@ -33,12 +34,14 @@ namespace ModernWpfPlayground.MvvmStuff
|
||||
protected T GetProperty<T>(T defaultValue = default, [CallerMemberName] string? propertyName = null)
|
||||
{
|
||||
if (propertyName == null) throw new ArgumentNullException(nameof(propertyName));
|
||||
return Values.TryGetValue(propertyName, out var obj) ? (T) obj : defaultValue;
|
||||
return Values.TryGetValue(propertyName, out var obj) ? (T)obj : defaultValue;
|
||||
}
|
||||
|
||||
protected void ResetViewModel()
|
||||
protected void ResetViewModel(Func<string, bool>? predicate = null)
|
||||
{
|
||||
foreach (var key in Values.Keys)
|
||||
IEnumerable<string> keys = _values.Keys;
|
||||
if (predicate != null) keys = keys.Where(predicate);
|
||||
foreach (var key in keys)
|
||||
{
|
||||
_values.Remove(key);
|
||||
RaisePropertyChanged(key);
|
||||
@ -49,10 +52,14 @@ namespace ModernWpfPlayground.MvvmStuff
|
||||
|
||||
protected void LoadViewModel()
|
||||
{
|
||||
var keysFromFile = new SortedSet<string>();
|
||||
foreach (var (key, value) in GetViewModelItems())
|
||||
{
|
||||
keysFromFile.Add(key);
|
||||
ObjectAccessor[key] = value;
|
||||
}
|
||||
|
||||
ResetViewModel(x => !keysFromFile.Contains(x));
|
||||
}
|
||||
}
|
||||
}
|
20
MvvmStuff/DeserializationExtension.cs
Normal file
20
MvvmStuff/DeserializationExtension.cs
Normal file
@ -0,0 +1,20 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
|
||||
namespace ModernWpfPlayground.MvvmStuff
|
||||
{
|
||||
public static class DeserializationExtension
|
||||
{
|
||||
public static object? Convert(object? value, Type propertyType)
|
||||
{
|
||||
if (value is null) return Activator.CreateInstance(propertyType);
|
||||
|
||||
if (propertyType.IsEnum && value is string s)
|
||||
{
|
||||
return Enum.Parse(propertyType, s);
|
||||
}
|
||||
|
||||
return System.Convert.ChangeType(value, propertyType, CultureInfo.InvariantCulture);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,40 +0,0 @@
|
||||
using System;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace ModernWpfPlayground.MvvmStuff
|
||||
{
|
||||
public static class PropertyInfoExtension
|
||||
{
|
||||
public static object? Convert(this JsonElement value, Type propertyType)
|
||||
{
|
||||
if (propertyType == null) return default;
|
||||
|
||||
if (propertyType == typeof(double))
|
||||
{
|
||||
return value.GetDouble();
|
||||
}
|
||||
|
||||
if (propertyType == typeof(bool))
|
||||
{
|
||||
return value.GetBoolean();
|
||||
}
|
||||
|
||||
if (propertyType == typeof(int))
|
||||
{
|
||||
return value.GetInt32();
|
||||
}
|
||||
|
||||
if (propertyType.IsEnum)
|
||||
{
|
||||
return Enum.ToObject(propertyType, value.GetInt32());
|
||||
}
|
||||
|
||||
if (propertyType == typeof(string))
|
||||
{
|
||||
return value.GetString();
|
||||
}
|
||||
|
||||
return default;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user