mirror of
https://github.com/holgerb83/ModernWpfPlayground.git
synced 2025-04-19 23:03:49 +02:00
improvements persisting view model properties
This commit is contained in:
parent
59d711da53
commit
046639681f
@ -8,7 +8,7 @@
|
|||||||
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="TaBEA 3.0.0"
|
Title="{Binding Title}"
|
||||||
Width="1200"
|
Width="1200"
|
||||||
Height="600"
|
Height="600"
|
||||||
ui:TitleBar.ExtendViewIntoTitleBar="True"
|
ui:TitleBar.ExtendViewIntoTitleBar="True"
|
||||||
|
@ -8,11 +8,11 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.6">
|
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.8">
|
||||||
<PrivateAssets>all</PrivateAssets>
|
<PrivateAssets>all</PrivateAssets>
|
||||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
</PackageReference>
|
</PackageReference>
|
||||||
<PackageReference Include="ModernWpfUI" Version="0.8.0" />
|
<PackageReference Include="ModernWpfUI" Version="0.8.1" />
|
||||||
<PackageReference Include="System.Drawing.Common" Version="4.7.0" />
|
<PackageReference Include="System.Drawing.Common" Version="4.7.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
@ -11,16 +11,17 @@ namespace ModernWpfPlayground.MvvmStuff
|
|||||||
{
|
{
|
||||||
public abstract class BaseViewModel : INotifyPropertyChanged
|
public abstract class BaseViewModel : INotifyPropertyChanged
|
||||||
{
|
{
|
||||||
|
private readonly Dictionary<string, object> _values = new Dictionary<string, object>();
|
||||||
|
|
||||||
private readonly Dictionary<string, object> _valueDict = new Dictionary<string, object>();
|
public IReadOnlyDictionary<string, object> Values => _values;
|
||||||
|
|
||||||
protected bool SetProperty<T>(T value, Action<T>? onChanged = null,
|
protected bool SetProperty<T>(T value, Action<T>? onChanged = null,
|
||||||
[CallerMemberName] string? propertyName = null)
|
[CallerMemberName] string? propertyName = null)
|
||||||
{
|
{
|
||||||
if(propertyName == null) throw new ArgumentNullException(nameof(propertyName));
|
if(propertyName == null) throw new ArgumentNullException(nameof(propertyName));
|
||||||
if (_valueDict.TryGetValue(propertyName, out var obj) && Equals(value, obj)) return false;
|
if (_values.TryGetValue(propertyName, out var obj) && Equals(value, obj)) return false;
|
||||||
|
|
||||||
_valueDict[propertyName] = value!;
|
_values[propertyName] = value!;
|
||||||
OnPropertyChanged(propertyName);
|
OnPropertyChanged(propertyName);
|
||||||
onChanged?.Invoke(value);
|
onChanged?.Invoke(value);
|
||||||
return true;
|
return true;
|
||||||
@ -29,7 +30,7 @@ namespace ModernWpfPlayground.MvvmStuff
|
|||||||
protected T GetProperty<T>(T defaultValue = default, [CallerMemberName] string? propertyName = null)
|
protected T GetProperty<T>(T defaultValue = default, [CallerMemberName] string? propertyName = null)
|
||||||
{
|
{
|
||||||
if (propertyName == null) throw new ArgumentNullException(nameof(propertyName));
|
if (propertyName == null) throw new ArgumentNullException(nameof(propertyName));
|
||||||
return _valueDict.TryGetValue(propertyName, out var obj) ? (T) obj : defaultValue;
|
return Values.TryGetValue(propertyName, out var obj) ? (T) obj : defaultValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
public event PropertyChangedEventHandler? PropertyChanged;
|
public event PropertyChangedEventHandler? PropertyChanged;
|
||||||
@ -42,32 +43,22 @@ namespace ModernWpfPlayground.MvvmStuff
|
|||||||
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void SaveViewModel()
|
|
||||||
{
|
|
||||||
var contents = JsonSerializer.Serialize(_valueDict);
|
|
||||||
var saveFileDialog = new SaveFileDialog {AddExtension = true, DefaultExt = "*.json"};
|
|
||||||
var result = saveFileDialog.ShowDialog(Application.Current.MainWindow?.Owner);
|
|
||||||
if (result == true)
|
|
||||||
{
|
|
||||||
File.WriteAllText(saveFileDialog.FileName, contents);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void ResetViewModel()
|
protected void ResetViewModel()
|
||||||
{
|
{
|
||||||
foreach (var key in _valueDict.Keys)
|
foreach (var key in Values.Keys)
|
||||||
{
|
{
|
||||||
_valueDict.Remove(key);
|
_values.Remove(key);
|
||||||
OnPropertyChanged(key);
|
OnPropertyChanged(key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void MapDictionary(IEnumerable<(string, object?)> tuples)
|
protected abstract IEnumerable<(string key, object? value)> GetViewModelItems();
|
||||||
|
|
||||||
|
protected void LoadViewModel()
|
||||||
{
|
{
|
||||||
if (tuples == null) throw new ArgumentNullException(nameof(tuples));
|
foreach (var (key, value) in GetViewModelItems())
|
||||||
foreach (var (key, value) in tuples)
|
|
||||||
{
|
{
|
||||||
_valueDict[key] = value!;
|
_values[key] = value!;
|
||||||
OnPropertyChanged(key);
|
OnPropertyChanged(key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,6 +15,31 @@ namespace ModernWpfPlayground
|
|||||||
{
|
{
|
||||||
private readonly PropertyInfo[] _properties;
|
private readonly PropertyInfo[] _properties;
|
||||||
private string? _path;
|
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()
|
public WindowViewModel()
|
||||||
{
|
{
|
||||||
@ -22,7 +47,11 @@ namespace ModernWpfPlayground
|
|||||||
CloseCommand = new RelayCommand(x => Application.Current.Shutdown());
|
CloseCommand = new RelayCommand(x => Application.Current.Shutdown());
|
||||||
OpenViewModelCommand = new RelayCommand(x => LoadViewModel());
|
OpenViewModelCommand = new RelayCommand(x => LoadViewModel());
|
||||||
SaveViewModelCommand = new RelayCommand(x => SaveViewModel());
|
SaveViewModelCommand = new RelayCommand(x => SaveViewModel());
|
||||||
ResetViewModelCommand = new RelayCommand(x => ResetViewModel());
|
ResetViewModelCommand = new RelayCommand(x =>
|
||||||
|
{
|
||||||
|
ResetViewModel();
|
||||||
|
Path = null;
|
||||||
|
});
|
||||||
_properties = GetType().GetProperties();
|
_properties = GetType().GetProperties();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -78,26 +107,6 @@ namespace ModernWpfPlayground
|
|||||||
|
|
||||||
public ICommand ResetViewModelCommand { get; }
|
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)
|
private object? CastToType(string key, JsonElement value)
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -106,6 +115,7 @@ namespace ModernWpfPlayground
|
|||||||
{
|
{
|
||||||
return default;
|
return default;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (property.PropertyType == typeof(double))
|
if (property.PropertyType == typeof(double))
|
||||||
{
|
{
|
||||||
return value.GetDouble();
|
return value.GetDouble();
|
||||||
@ -133,5 +143,33 @@ namespace ModernWpfPlayground
|
|||||||
|
|
||||||
return default;
|
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));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user