added theme mode to view and object accessor for setting properties

This commit is contained in:
Holger Börchers 2020-04-09 21:57:49 +02:00
parent 365537102f
commit 59fe84a53f
6 changed files with 50 additions and 20 deletions

View File

@ -39,7 +39,7 @@
<!-- TitleBar --> <!-- TitleBar -->
<Grid <Grid
Height="{Binding ElementName=Window, Path=(ui:TitleBar.Height)}" Height="{Binding ElementName=Window, Path=(ui:TitleBar.Height)}"
Background="#DDDDDD" Background="{DynamicResource SystemControlBackgroundChromeMediumBrush}"
DockPanel.Dock="Top"> DockPanel.Dock="Top">
<Grid.Style> <Grid.Style>
<Style TargetType="Grid"> <Style TargetType="Grid">
@ -185,7 +185,6 @@
<TreeView <TreeView
Grid.Column="1" Grid.Column="1"
MinWidth="180" MinWidth="180"
Background="#F3F3F3"
BorderThickness="0"> BorderThickness="0">
<TreeViewItem Header="Root" IsExpanded="True"> <TreeViewItem Header="Root" IsExpanded="True">
<TreeViewItem Header="Child1" /> <TreeViewItem Header="Child1" />
@ -219,6 +218,7 @@
</TabItem> </TabItem>
<TabItem Header="General" IsSelected="True"> <TabItem Header="General" IsSelected="True">
<ui:SimpleStackPanel Margin="5" Spacing="10"> <ui:SimpleStackPanel Margin="5" Spacing="10">
<propertyPresenter2:PropertyPresenter2 Label="Theme Mode" Value="{Binding ThemeMode}" />
<propertyPresenter2:PropertyPresenter2 <propertyPresenter2:PropertyPresenter2
Command="{Binding ShowDialogCommand}" Command="{Binding ShowDialogCommand}"
Label="Hello" Label="Hello"

View File

@ -8,7 +8,8 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="ModernWpfUI" Version="0.8.1" /> <PackageReference Include="FastMember" Version="1.5.0" />
<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="System.Drawing.Common" Version="4.7.0" /> <PackageReference Include="System.Drawing.Common" Version="4.7.0" />

View File

@ -1,13 +1,20 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using FastMember;
using Prism.Mvvm; using Prism.Mvvm;
namespace ModernWpfPlayground.MvvmStuff namespace ModernWpfPlayground.MvvmStuff
{ {
public abstract class BaseViewModel : BindableBase public abstract class BaseViewModel : BindableBase
{ {
protected BaseViewModel()
{
ObjectAccessor = ObjectAccessor.Create(this);
}
private readonly Dictionary<string, object> _values = new Dictionary<string, object>(); private readonly Dictionary<string, object> _values = new Dictionary<string, object>();
protected readonly ObjectAccessor ObjectAccessor;
protected IReadOnlyDictionary<string, object> Values => _values; protected IReadOnlyDictionary<string, object> Values => _values;
@ -44,8 +51,7 @@ namespace ModernWpfPlayground.MvvmStuff
{ {
foreach (var (key, value) in GetViewModelItems()) foreach (var (key, value) in GetViewModelItems())
{ {
_values[key] = value!; ObjectAccessor[key] = value;
RaisePropertyChanged(key);
} }
} }
} }

View File

@ -1,38 +1,35 @@
using System; using System;
using System.Reflection;
using System.Text.Json; using System.Text.Json;
namespace ModernWpfPlayground.MvvmStuff namespace ModernWpfPlayground.MvvmStuff
{ {
public static class PropertyInfoExtension public static class PropertyInfoExtension
{ {
public static PropertyInfo? Find(this PropertyInfo[] infos, string key) => Array.Find(infos, x => x.Name == key); public static object? Convert(this JsonElement value, Type propertyType)
public static object? Convert(this PropertyInfo? propertyInfo, JsonElement value)
{ {
if (propertyInfo == null) return default; if (propertyType == null) return default;
if (propertyInfo.PropertyType == typeof(double)) if (propertyType == typeof(double))
{ {
return value.GetDouble(); return value.GetDouble();
} }
if (propertyInfo.PropertyType == typeof(bool)) if (propertyType == typeof(bool))
{ {
return value.GetBoolean(); return value.GetBoolean();
} }
if (propertyInfo.PropertyType == typeof(int)) if (propertyType == typeof(int))
{ {
return value.GetInt32(); return value.GetInt32();
} }
if (propertyInfo.PropertyType.IsEnum) if (propertyType.IsEnum)
{ {
return Enum.ToObject(propertyInfo.PropertyType, value.GetInt32()); return Enum.ToObject(propertyType, value.GetInt32());
} }
if (propertyInfo.PropertyType == typeof(string)) if (propertyType == typeof(string))
{ {
return value.GetString(); return value.GetString();
} }

11
ThemeMode.cs Normal file
View File

@ -0,0 +1,11 @@
using System.ComponentModel;
namespace ModernWpfPlayground
{
public enum ThemeMode
{
[Description("Light")] Light,
[Description("Dark")] Dark,
[Description("Use system setting")] UseSystemSetting
}
}

View File

@ -1,11 +1,11 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Reflection;
using System.Text.Json; 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;
using Microsoft.Win32; using Microsoft.Win32;
using ModernWpf;
using ModernWpfPlayground.MvvmStuff; using ModernWpfPlayground.MvvmStuff;
using Prism.Commands; using Prism.Commands;
@ -14,7 +14,6 @@ namespace ModernWpfPlayground
public class WindowViewModel : BaseViewModel public class WindowViewModel : BaseViewModel
{ {
private const string AppName = "TaBEA 3.0.0"; private const string AppName = "TaBEA 3.0.0";
private readonly PropertyInfo[] _properties;
private string? _path; private string? _path;
private string _title = AppName; private string _title = AppName;
@ -29,7 +28,6 @@ namespace ModernWpfPlayground
ResetViewModel(); ResetViewModel();
Path = null; Path = null;
}); });
_properties = GetType().GetProperties();
} }
public string? Path public string? Path
@ -85,6 +83,23 @@ namespace ModernWpfPlayground
public ICommand ResetViewModelCommand { get; } public ICommand ResetViewModelCommand { get; }
public ThemeMode ThemeMode
{
get => GetProperty(ThemeMode.UseSystemSetting);
set => SetProperty(value, SetTheme);
}
private static void SetTheme(ThemeMode themeMode)
{
ThemeManager.Current.ApplicationTheme = themeMode switch
{
ThemeMode.Light => ApplicationTheme.Light,
ThemeMode.Dark => ApplicationTheme.Dark,
ThemeMode.UseSystemSetting => default,
_ => ThemeManager.Current.ApplicationTheme
};
}
private async Task ShowDialogAsync() private async Task ShowDialogAsync()
{ {
var dialog = new ContentDialogExample {Message = WelcomeMessage}; var dialog = new ContentDialogExample {Message = WelcomeMessage};
@ -121,7 +136,7 @@ namespace ModernWpfPlayground
var obj = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(contents); var obj = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(contents);
foreach (var (key, value) in obj) foreach (var (key, value) in obj)
{ {
yield return (key, _properties.Find(key).Convert(value)); yield return (key, value.Convert(ObjectAccessor[key].GetType()));
} }
} }
} }