mirror of
https://github.com/holgerb83/ModernWpfPlayground.git
synced 2025-12-14 16:16:24 +01:00
improvements persisting view model properties
This commit is contained in:
@@ -15,6 +15,31 @@ namespace ModernWpfPlayground
|
||||
{
|
||||
private readonly PropertyInfo[] _properties;
|
||||
private string? _path;
|
||||
private string _title = AppName;
|
||||
private const string AppName = "TaBEA 3.0.0";
|
||||
|
||||
public string? Path
|
||||
{
|
||||
get => _path;
|
||||
private set
|
||||
{
|
||||
if (Equals(_path, value)) return;
|
||||
_path = value;
|
||||
OnPropertyChanged();
|
||||
Title = value != null ? $"{System.IO.Path.GetFileName(value)} - {AppName}" : AppName;
|
||||
}
|
||||
}
|
||||
|
||||
public string Title
|
||||
{
|
||||
get => _title;
|
||||
set
|
||||
{
|
||||
if (Equals(_title, value)) return;
|
||||
_title = value;
|
||||
OnPropertyChanged();
|
||||
}
|
||||
}
|
||||
|
||||
public WindowViewModel()
|
||||
{
|
||||
@@ -22,7 +47,11 @@ namespace ModernWpfPlayground
|
||||
CloseCommand = new RelayCommand(x => Application.Current.Shutdown());
|
||||
OpenViewModelCommand = new RelayCommand(x => LoadViewModel());
|
||||
SaveViewModelCommand = new RelayCommand(x => SaveViewModel());
|
||||
ResetViewModelCommand = new RelayCommand(x => ResetViewModel());
|
||||
ResetViewModelCommand = new RelayCommand(x =>
|
||||
{
|
||||
ResetViewModel();
|
||||
Path = null;
|
||||
});
|
||||
_properties = GetType().GetProperties();
|
||||
}
|
||||
|
||||
@@ -78,26 +107,6 @@ namespace ModernWpfPlayground
|
||||
|
||||
public ICommand ResetViewModelCommand { get; }
|
||||
|
||||
private void LoadViewModel()
|
||||
{
|
||||
var list = ReadFromJson();
|
||||
MapDictionary(list);
|
||||
}
|
||||
|
||||
private IEnumerable<(string, object?)> ReadFromJson()
|
||||
{
|
||||
var openFileDialog = new OpenFileDialog { AddExtension = true, DefaultExt = "*.json" };
|
||||
var result = openFileDialog.ShowDialog(Application.Current.MainWindow?.Owner);
|
||||
if (result != true) yield break;
|
||||
|
||||
var contents = File.ReadAllText(_path = openFileDialog.FileName);
|
||||
var obj = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(contents);
|
||||
foreach (var (key, value) in obj)
|
||||
{
|
||||
yield return (key, CastToType(key, value));
|
||||
}
|
||||
}
|
||||
|
||||
private object? CastToType(string key, JsonElement value)
|
||||
{
|
||||
|
||||
@@ -106,6 +115,7 @@ namespace ModernWpfPlayground
|
||||
{
|
||||
return default;
|
||||
}
|
||||
|
||||
if (property.PropertyType == typeof(double))
|
||||
{
|
||||
return value.GetDouble();
|
||||
@@ -133,5 +143,33 @@ namespace ModernWpfPlayground
|
||||
|
||||
return default;
|
||||
}
|
||||
|
||||
private void SaveViewModel()
|
||||
{
|
||||
var contents = JsonSerializer.Serialize(Values);
|
||||
if (Path == null)
|
||||
{
|
||||
var saveFileDialog = new SaveFileDialog {AddExtension = true, DefaultExt = "*.json"};
|
||||
var result = saveFileDialog.ShowDialog(Application.Current.MainWindow?.Owner);
|
||||
if (result != true) return;
|
||||
Path = saveFileDialog.FileName;
|
||||
}
|
||||
|
||||
File.WriteAllText(Path, contents);
|
||||
}
|
||||
|
||||
protected override IEnumerable<(string key, object? value)> GetViewModelItems()
|
||||
{
|
||||
var openFileDialog = new OpenFileDialog {AddExtension = true, DefaultExt = "*.json"};
|
||||
var result = openFileDialog.ShowDialog(Application.Current.MainWindow?.Owner);
|
||||
if (result != true) yield break;
|
||||
|
||||
var contents = File.ReadAllText(Path = openFileDialog.FileName);
|
||||
var obj = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(contents);
|
||||
foreach (var (key, value) in obj)
|
||||
{
|
||||
yield return (key, CastToType(key, value));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user