mirror of
https://github.com/holgerb83/ModernWpfPlayground.git
synced 2025-06-30 09:40:52 +02:00
Reorganization of files
This commit is contained in:
65
App/MvvmStuff/BaseViewModel.cs
Normal file
65
App/MvvmStuff/BaseViewModel.cs
Normal file
@ -0,0 +1,65 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
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;
|
||||
|
||||
protected bool SetProperty<T>(T value, Action<T>? onChanged = null,
|
||||
[CallerMemberName] string? propertyName = null)
|
||||
{
|
||||
if (propertyName == null) throw new ArgumentNullException(nameof(propertyName));
|
||||
if (_values.TryGetValue(propertyName, out var obj) && Equals(value, obj)) return false;
|
||||
|
||||
_values[propertyName] = value!;
|
||||
RaisePropertyChanged(propertyName);
|
||||
onChanged?.Invoke(value);
|
||||
return true;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
protected void ResetViewModel(Func<string, bool>? predicate = null)
|
||||
{
|
||||
IEnumerable<string> keys = _values.Keys;
|
||||
if (predicate != null) keys = keys.Where(predicate);
|
||||
foreach (var key in keys)
|
||||
{
|
||||
_values.Remove(key);
|
||||
RaisePropertyChanged(key);
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract IEnumerable<(string key, object? value)> GetViewModelItems();
|
||||
|
||||
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
App/MvvmStuff/DeserializationExtension.cs
Normal file
20
App/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);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user