enabled nullable

This commit is contained in:
Holger Börchers 2020-02-22 22:31:19 +01:00
parent 153a9c3986
commit 0ebad36335
5 changed files with 96 additions and 57 deletions

View File

@ -4,9 +4,14 @@
<OutputType>WinExe</OutputType> <OutputType>WinExe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework> <TargetFramework>netcoreapp3.1</TargetFramework>
<UseWPF>true</UseWPF> <UseWPF>true</UseWPF>
<Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.6">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="ModernWpfUI" Version="0.8.0" /> <PackageReference Include="ModernWpfUI" Version="0.8.0" />
<PackageReference Include="System.Drawing.Common" Version="4.7.0" /> <PackageReference Include="System.Drawing.Common" Version="4.7.0" />
</ItemGroup> </ItemGroup>

View File

@ -2,7 +2,6 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.IO; using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Text.Json; using System.Text.Json;
using System.Windows; using System.Windows;
@ -12,34 +11,34 @@ namespace ModernWpfPlayground.MvvmStuff
{ {
public abstract class BaseViewModel : INotifyPropertyChanged public abstract class BaseViewModel : INotifyPropertyChanged
{ {
protected BaseViewModel()
{
_properties = GetType().GetProperties();
}
private readonly Dictionary<string, object> _valueDict = new Dictionary<string, object>(); private readonly Dictionary<string, object> _valueDict = new Dictionary<string, object>();
private readonly PropertyInfo[] _properties;
protected bool SetProperty<T>(T value, [CallerMemberName] string propertyName = null) protected bool SetProperty<T>(T value, Action<T>? onChanged = null,
[CallerMemberName] string? propertyName = null)
{ {
if(propertyName == null) throw new ArgumentNullException(nameof(propertyName));
if (_valueDict.TryGetValue(propertyName, out var obj) && Equals(value, obj)) return false; if (_valueDict.TryGetValue(propertyName, out var obj) && Equals(value, obj)) return false;
_valueDict[propertyName] = value; _valueDict[propertyName] = value!;
OnPropertyChanged(propertyName); OnPropertyChanged(propertyName);
onChanged?.Invoke(value);
return true; return true;
} }
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));
return _valueDict.TryGetValue(propertyName, out var obj) ? (T) obj : defaultValue; return _valueDict.TryGetValue(propertyName, out var obj) ? (T) obj : defaultValue;
} }
public event PropertyChangedEventHandler PropertyChanged; public event PropertyChangedEventHandler? PropertyChanged;
[NotifyPropertyChangedInvocator] [NotifyPropertyChangedInvocator]
[PublicAPI] [PublicAPI]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
{ {
if (propertyName == null) throw new ArgumentNullException(nameof(propertyName));
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
} }
@ -63,50 +62,14 @@ namespace ModernWpfPlayground.MvvmStuff
} }
} }
protected void LoadViewModel() protected void MapDictionary(IEnumerable<(string, object?)> tuples)
{ {
var openFileDialog = new OpenFileDialog {AddExtension = true, DefaultExt = "*.json"}; if (tuples == null) throw new ArgumentNullException(nameof(tuples));
var result = openFileDialog.ShowDialog(Application.Current.MainWindow?.Owner); foreach (var (key, value) in tuples)
if (result != true) return;
var contents = File.ReadAllText(openFileDialog.FileName);
var obj = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(contents);
foreach (var (key, value) in obj)
{ {
_valueDict[key] = CastToType(key, value); _valueDict[key] = value!;
OnPropertyChanged(key); 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;
}
} }
} }

View File

@ -5,7 +5,7 @@ namespace ModernWpfPlayground.MvvmStuff
{ {
public class RelayCommand : ICommand public class RelayCommand : ICommand
{ {
private readonly Predicate<object> _canExecute; private readonly Predicate<object>? _canExecute;
private readonly Action<object> _execute; private readonly Action<object> _execute;
public RelayCommand(Action<object> execute) public RelayCommand(Action<object> execute)

View File

@ -16,7 +16,7 @@ namespace ModernWpfPlayground.PropertyPresenter2
/// <summary> /// <summary>
/// Makes an Bitmap from every Imageformat. /// Makes an Bitmap from every Imageformat.
/// </summary> /// </summary>
public class ObjectImageConverter : IValueConverter public sealed class ObjectImageConverter : IValueConverter
{ {
object IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo culture) object IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo culture)
{ {

View File

@ -1,32 +1,47 @@
using System.Threading.Tasks; using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text.Json;
using System.Threading.Tasks;
using System.Windows; using System.Windows;
using System.Windows.Input; using System.Windows.Input;
using Microsoft.Win32;
using ModernWpfPlayground.MvvmStuff; using ModernWpfPlayground.MvvmStuff;
namespace ModernWpfPlayground namespace ModernWpfPlayground
{ {
public class WindowViewModel : BaseViewModel public class WindowViewModel : BaseViewModel
{ {
private readonly PropertyInfo[] _properties;
private string? _path;
public WindowViewModel() public WindowViewModel()
{ {
ShowDialogCommand = new RelayCommand(async x => await ShowDialogAsync()); ShowDialogCommand = new RelayCommand(async x => await ShowDialogAsync().ConfigureAwait(false));
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());
_properties = GetType().GetProperties();
} }
private async Task ShowDialogAsync() private async Task ShowDialogAsync()
{ {
var dialog = new ContentDialogExample {Message = WelcomeMessage}; var dialog = new ContentDialogExample {Message = WelcomeMessage};
var result = await dialog.ShowAsync(); var result = await dialog.ShowAsync().ConfigureAwait(false);
WelcomeMessage = result.ToString(); WelcomeMessage = result.ToString();
} }
public bool BooleanValue public bool BooleanValue
{ {
get => GetProperty(true); get => GetProperty(true);
set => SetProperty(value); set => SetProperty(value, BooleanValue_OnChanged);
}
private void BooleanValue_OnChanged(bool obj)
{
VisibilityEnumTest = obj ? Visibility.Visible : Visibility.Collapsed;
} }
public Visibility VisibilityEnumTest public Visibility VisibilityEnumTest
@ -62,5 +77,61 @@ namespace ModernWpfPlayground
public ICommand SaveViewModelCommand { get; } public ICommand SaveViewModelCommand { get; }
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)
{
var property = Array.Find(_properties, x => x.Name == key);
if (property == null)
{
return default;
}
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;
}
} }
} }