From 6ba9c57bd0493b13922882faf1912a6aebb6d011 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Holger=20B=C3=B6rchers?= Date: Tue, 18 Feb 2020 22:07:39 +0100 Subject: [PATCH] persisting view models --- BaseViewModel.cs | 66 +++++++++++++++++++++++++++++++++++++++++----- MainWindow.xaml | 43 +++++++++++++++++++++++++----- WindowViewModel.cs | 9 +++++++ 3 files changed, 104 insertions(+), 14 deletions(-) diff --git a/BaseViewModel.cs b/BaseViewModel.cs index 1f4038c..741fcd4 100644 --- a/BaseViewModel.cs +++ b/BaseViewModel.cs @@ -1,6 +1,8 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.ComponentModel; using System.IO; +using System.Reflection; using System.Runtime.CompilerServices; using System.Text.Json; using System.Windows; @@ -11,7 +13,13 @@ namespace ModernWpfPlayground { public abstract class BaseViewModel : INotifyPropertyChanged { + protected BaseViewModel() + { + _properties = GetType().GetProperties(); + } + private readonly Dictionary _valueDict = new Dictionary(); + private readonly PropertyInfo[] _properties; protected bool SetProperty(T value, [CallerMemberName] string propertyName = null) { @@ -39,7 +47,7 @@ namespace ModernWpfPlayground public void SaveViewModel() { var contents = JsonSerializer.Serialize(_valueDict); - var saveFileDialog = new SaveFileDialog(); + var saveFileDialog = new SaveFileDialog {AddExtension = true, DefaultExt = "*.json"}; var result = saveFileDialog.ShowDialog(Application.Current.MainWindow?.Owner); if (result == true) { @@ -47,15 +55,59 @@ namespace ModernWpfPlayground } } - public void LoadViewModel(string path) + public void ResetViewModel() { - var contents = File.ReadAllText(path); - var obj = JsonSerializer.Deserialize>(contents); - foreach (var (key, value) in obj) + foreach (var key in _valueDict.Keys) { - _valueDict[key] = value; + _valueDict.Remove(key); OnPropertyChanged(key); } } + + public void LoadViewModel() + { + var openFileDialog = new OpenFileDialog {AddExtension = true, DefaultExt = "*.json"}; + var result = openFileDialog.ShowDialog(Application.Current.MainWindow?.Owner); + if (result != true) return; + var contents = File.ReadAllText(openFileDialog.FileName); + var obj = JsonSerializer.Deserialize>(contents); + foreach (var (key, value) in obj) + { + _valueDict[key] = CastToType(key, value); + OnPropertyChanged(key); + } + } + + private object CastToType(string key, JsonElement value) + { + + var property = Array.Find(_properties, x => x.Name == key); + if (property.PropertyType == typeof(double)) + { + return value.GetDouble(); + } + + if (property.PropertyType == typeof(bool)) + { + return value.GetBoolean(); + } + + if (property.PropertyType == typeof(int)) + { + return value.GetInt32(); + } + + if (property.PropertyType.IsEnum) + { + return Enum.ToObject(property.PropertyType, value.GetInt32()); + } + + if (property.PropertyType == typeof(string)) + { + return value.GetString(); + } + + return default; + } } } \ No newline at end of file diff --git a/MainWindow.xaml b/MainWindow.xaml index a60c3f2..ea7cb01 100644 --- a/MainWindow.xaml +++ b/MainWindow.xaml @@ -22,6 +22,21 @@ + + + + + + - - + + + @@ -172,14 +197,18 @@ Margin="5" Orientation="Vertical" Spacing="5"> -