switched to yaml as filee format

This commit is contained in:
Holger Börchers 2020-04-17 22:53:19 +02:00
parent 6cc2470a78
commit 8cb33b89fb
9 changed files with 54 additions and 59 deletions

View File

@ -11,7 +11,7 @@ namespace ModernWpfPlayground
protected override void RegisterTypes(IContainerRegistry containerRegistry) protected override void RegisterTypes(IContainerRegistry containerRegistry)
{ {
containerRegistry.Register<MainWindow>(); containerRegistry.Register<MainWindow>();
containerRegistry.Register<WindowViewModel>(); containerRegistry.Register<MainWindowViewModel>();
} }
protected override Window CreateShell() protected override Window CreateShell()

View File

@ -1,17 +1,20 @@
<Window 
<Window
x:Class="ModernWpfPlayground.MainWindow" x:Class="ModernWpfPlayground.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:ModernWpfPlayground" xmlns:local="clr-namespace:ModernWpfPlayground"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:prism="http://prismlibrary.com/"
xmlns:propertyPresenter2="clr-namespace:ModernWpfPlayground.PropertyPresenter2" xmlns:propertyPresenter2="clr-namespace:ModernWpfPlayground.PropertyPresenter2"
xmlns:ui="http://schemas.modernwpf.com/2019" xmlns:ui="http://schemas.modernwpf.com/2019"
x:Name="Window" x:Name="Window"
Title="{Binding Title}" Title="{Binding Title}"
Width="1200" Width="1200"
Height="600" Height="600"
d:DataContext="{d:DesignInstance local:WindowViewModel}" d:DataContext="{d:DesignInstance local:MainWindowViewModel}"
prism:ViewModelLocator.AutoWireViewModel="True"
ui:ThemeManager.IsThemeAware="True" ui:ThemeManager.IsThemeAware="True"
ui:TitleBar.ExtendViewIntoTitleBar="True" ui:TitleBar.ExtendViewIntoTitleBar="True"
ui:WindowHelper.UseModernWindowStyle="True" ui:WindowHelper.UseModernWindowStyle="True"

View File

@ -5,10 +5,9 @@
/// </summary> /// </summary>
public partial class MainWindow public partial class MainWindow
{ {
public MainWindow(WindowViewModel viewModel) public MainWindow()
{ {
InitializeComponent(); InitializeComponent();
DataContext = viewModel;
} }
} }
} }

View File

@ -1,6 +1,5 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Text.Json;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows; using System.Windows;
using System.Windows.Input; using System.Windows.Input;
@ -8,16 +7,19 @@ using Microsoft.Win32;
using ModernWpf; using ModernWpf;
using ModernWpfPlayground.MvvmStuff; using ModernWpfPlayground.MvvmStuff;
using Prism.Commands; using Prism.Commands;
using YamlDotNet.Serialization;
namespace ModernWpfPlayground namespace ModernWpfPlayground
{ {
public class WindowViewModel : BaseViewModel public class MainWindowViewModel : BaseViewModel
{ {
private const string AppName = "TaBEA 3.0.0"; private const string AppName = "TaBEA 3.0.0";
private string? _path; private string? _path;
private string _title = AppName; private string _title = AppName;
private readonly ISerializer _serializer;
private readonly IDeserializer _deserializer;
public WindowViewModel() public MainWindowViewModel()
{ {
ShowDialogCommand = new DelegateCommand(async () => await ShowDialogAsync().ConfigureAwait(false)); ShowDialogCommand = new DelegateCommand(async () => await ShowDialogAsync().ConfigureAwait(false));
CloseCommand = new DelegateCommand(() => Application.Current.Shutdown()); CloseCommand = new DelegateCommand(() => Application.Current.Shutdown());
@ -28,6 +30,8 @@ namespace ModernWpfPlayground
ResetViewModel(); ResetViewModel();
Path = null; Path = null;
}); });
_serializer = new SerializerBuilder().Build();
_deserializer = new DeserializerBuilder().Build();
} }
public string? Path public string? Path
@ -114,10 +118,10 @@ namespace ModernWpfPlayground
private void SaveViewModel() private void SaveViewModel()
{ {
var contents = JsonSerializer.Serialize(Values); var contents = _serializer.Serialize(Values);
if (Path == null) if (Path == null)
{ {
var saveFileDialog = new SaveFileDialog {AddExtension = true, DefaultExt = "*.json"}; var saveFileDialog = new SaveFileDialog {AddExtension = true, DefaultExt = "*.yaml"};
var result = saveFileDialog.ShowDialog(Application.Current.MainWindow?.Owner); var result = saveFileDialog.ShowDialog(Application.Current.MainWindow?.Owner);
if (result != true) return; if (result != true) return;
Path = saveFileDialog.FileName; Path = saveFileDialog.FileName;
@ -128,15 +132,16 @@ namespace ModernWpfPlayground
protected override IEnumerable<(string key, object? value)> GetViewModelItems() protected override IEnumerable<(string key, object? value)> GetViewModelItems()
{ {
var openFileDialog = new OpenFileDialog {AddExtension = true, DefaultExt = "*.json"}; var openFileDialog = new OpenFileDialog {AddExtension = true, DefaultExt = "*.yaml"};
var result = openFileDialog.ShowDialog(Application.Current.MainWindow?.Owner); var result = openFileDialog.ShowDialog(Application.Current.MainWindow?.Owner);
if (result != true) yield break; if (result != true) yield break;
var contents = File.ReadAllText(Path = openFileDialog.FileName); var contents = File.ReadAllText(Path = openFileDialog.FileName);
var obj = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(contents);
var obj = _deserializer.Deserialize<Dictionary<string, object>>(contents);
foreach (var (key, value) in obj) foreach (var (key, value) in obj)
{ {
yield return (key, value.Convert(ObjectAccessor[key].GetType())); yield return (key, DeserializationExtension.Convert(value, ObjectAccessor[key].GetType()));
} }
} }
} }

View File

@ -12,6 +12,7 @@
<PackageReference Include="ModernWpfUI" Version="0.8.2" /> <PackageReference Include="ModernWpfUI" Version="0.8.2" />
<PackageReference Include="Prism.DryIoc" Version="7.2.0.1422" /> <PackageReference Include="Prism.DryIoc" Version="7.2.0.1422" />
<PackageReference Include="Prism.Wpf" Version="7.2.0.1422" /> <PackageReference Include="Prism.Wpf" Version="7.2.0.1422" />
<PackageReference Include="YamlDotNet" Version="8.1.0" />
<PackageReference Include="System.Drawing.Common" Version="4.7.0" /> <PackageReference Include="System.Drawing.Common" Version="4.7.0" />
</ItemGroup> </ItemGroup>

View File

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using FastMember; using FastMember;
using Prism.Mvvm; using Prism.Mvvm;
@ -36,9 +37,11 @@ namespace ModernWpfPlayground.MvvmStuff
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); _values.Remove(key);
RaisePropertyChanged(key); RaisePropertyChanged(key);
@ -49,10 +52,14 @@ namespace ModernWpfPlayground.MvvmStuff
protected void LoadViewModel() protected void LoadViewModel()
{ {
var keysFromFile = new SortedSet<string>();
foreach (var (key, value) in GetViewModelItems()) foreach (var (key, value) in GetViewModelItems())
{ {
keysFromFile.Add(key);
ObjectAccessor[key] = value; ObjectAccessor[key] = value;
} }
ResetViewModel(x => !keysFromFile.Contains(x));
} }
} }
} }

View 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);
}
}
}

View File

@ -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;
}
}
}