From 48d244ea2011274ef5b572ed6ac847662f00e245 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Holger=20B=C3=B6rchers?= Date: Mon, 26 Jul 2021 21:33:36 +0200 Subject: [PATCH] roundup --- .vscode/launch.json | 2 +- App/App.xaml | 8 +- App/App.xaml.cs | 13 --- App/MainWindow.xaml | 14 +-- App/MainWindowViewModel.cs | 184 ++++++++++----------------------- App/ModernWpfPlayground.csproj | 14 ++- App/MvvmStuff/BaseViewModel.cs | 67 ------------ Controls/Controls.csproj | 2 +- 8 files changed, 76 insertions(+), 228 deletions(-) delete mode 100644 App/MvvmStuff/BaseViewModel.cs diff --git a/.vscode/launch.json b/.vscode/launch.json index 982dc68..79a5772 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -10,7 +10,7 @@ "request": "launch", "preLaunchTask": "build", // If you have changed target frameworks, make sure to update the program path. - "program": "${workspaceFolder}/App/bin/Debug/net5.0-windows10.0.18362.0/ModernWpfPlayground.dll", + "program": "${workspaceFolder}/App/bin/Debug/net5.0-windows10.0.18362.0/win-x64/ModernWpfPlayground.dll", "args": [], "cwd": "${workspaceFolder}", // For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console diff --git a/App/App.xaml b/App/App.xaml index 737612a..e4ada20 100644 --- a/App/App.xaml +++ b/App/App.xaml @@ -1,9 +1,9 @@ - + xmlns:ui="http://schemas.modernwpf.com/2019" + StartupUri="MainWindow.xaml"> @@ -22,4 +22,4 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/App/App.xaml.cs b/App/App.xaml.cs index f81f2d0..2fd2bdb 100644 --- a/App/App.xaml.cs +++ b/App/App.xaml.cs @@ -1,7 +1,6 @@ using System.Windows; using ModernWpf; using ModernWpfPlayground.Types; -using Prism.Ioc; namespace ModernWpfPlayground { @@ -16,17 +15,5 @@ namespace ModernWpfPlayground base.OnStartup(e); ThemeManager.Current.AccentColor = AccentColors.Green.ToWindowsColor(); } - - - protected override void RegisterTypes(IContainerRegistry containerRegistry) - { - containerRegistry.Register(); - containerRegistry.Register(); - } - - protected override Window CreateShell() - { - return Container.Resolve(); - } } } diff --git a/App/MainWindow.xaml b/App/MainWindow.xaml index c67964a..c29e8b9 100644 --- a/App/MainWindow.xaml +++ b/App/MainWindow.xaml @@ -7,20 +7,21 @@ xmlns:iconPacks="http://metro.mahapps.com/winfx/xaml/iconpacks" xmlns:local="clr-namespace:ModernWpfPlayground" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" - xmlns:prism="http://prismlibrary.com/" xmlns:ui="http://schemas.modernwpf.com/2019" x:Name="Window" Title="{Binding Title}" Width="{Binding WindowWidth, Mode=TwoWay}" Height="{Binding WindowHeight, Mode=TwoWay}" d:DataContext="{d:DesignInstance local:MainWindowViewModel}" - prism:ViewModelLocator.AutoWireViewModel="True" ui:ThemeManager.IsThemeAware="True" ui:TitleBar.ExtendViewIntoTitleBar="True" ui:WindowHelper.UseModernWindowStyle="True" TextOptions.TextFormattingMode="Display" UseLayoutRounding="True" mc:Ignorable="d"> + + + @@ -219,10 +220,8 @@ - + - - @@ -246,7 +245,10 @@ - + diff --git a/App/MainWindowViewModel.cs b/App/MainWindowViewModel.cs index 5629ef0..9e29762 100644 --- a/App/MainWindowViewModel.cs +++ b/App/MainWindowViewModel.cs @@ -1,166 +1,88 @@ -using System.Collections.Generic; -using System.IO; -using System.Threading.Tasks; +using ModernWpfPlayground.Types; +using MvvmGen; using System.Windows; -using System.Windows.Input; -using Microsoft.Win32; -using ModernWpfPlayground.MvvmStuff; -using ModernWpfPlayground.Types; -using Prism.Commands; -using YamlDotNet.Serialization; using static ModernWpf.ThemeManager; namespace ModernWpfPlayground { // ReSharper disable once ClassNeverInstantiated.Global - public class MainWindowViewModel : BaseViewModel + [ViewModel] + public partial class MainWindowViewModel { private const string AppName = "TaBEA 3.0.0"; + + [Property, PropertyCallMethod(nameof(SetTitle))] private string? _path; - private string _title = AppName; - private readonly ISerializer _serializer; - private readonly IDeserializer _deserializer; - public MainWindowViewModel() + [Property] private string _title = AppName; + + [Property, PropertyCallMethod(nameof(BooleanValue_OnChanged))] + private bool _booleanValue = true; + + [Property] private Visibility _visibilityEnumTest = Visibility.Visible; + [Property] private double _sliderTest = 100; + [Property] private double _validationTest; + [Property] private string? _welcomeMessage = "Shadow of the empire"; + + [Property, PropertyCallMethod(nameof(SetTheme))] + private ThemeMode _themeMode = ThemeMode.UseSystemSetting; + + [Property, PropertyCallMethod(nameof(SetAccentColor))] + private AccentColors _accentColors = AccentColors.Green; + + [Property] private int _windowWidth = 1200; + [Property] private int _windowHeight = 600; + [Property] private bool _isPaneOpen = true; + + + [Command] + private void ShowNotification() { - ShowDialogCommand = new DelegateCommand(ShowDialog); - CloseCommand = new DelegateCommand(() => Application.Current.Shutdown()); - OpenViewModelCommand = new DelegateCommand(LoadViewModel); - SaveViewModelCommand = new DelegateCommand(SaveViewModel); - ResetViewModelCommand = new DelegateCommand(() => - { - ResetViewModel(); - Path = null; - }); - _serializer = new SerializerBuilder().Build(); - _deserializer = new DeserializerBuilder().Build(); } - private string? Path + [Command] + private void Close() { - get => _path; - set => SetProperty(ref _path, value, - () => Title = value != null ? $"{System.IO.Path.GetFileName(value)} - {AppName}" : AppName); + Application.Current.MainWindow?.Close(); } - public string Title + private void SetTitle() { - get => _title; - set => SetProperty(ref _title, value); + Title = Path != null ? $"{System.IO.Path.GetFileName(Path)} - {AppName}" : AppName; } - public bool BooleanValue - { - get => GetProperty(true); - set => SetProperty(value, BooleanValue_OnChanged); - } + private void SetAccentColor() => Current.AccentColor = AccentColors.ToWindowsColor(); - public Visibility VisibilityEnumTest - { - get => GetProperty(Visibility.Visible); - set => SetProperty(value); - } - public double SliderTest - { - get => GetProperty(100D); - set => SetProperty(value); - } + private void SetTheme() => Current.ApplicationTheme = ThemeMode.ToApplicationTheme(); - public double ValidationTest - { - get => GetProperty(0D); - set => SetProperty(value); - } - - public ICommand ShowDialogCommand { get; } - - public string? WelcomeMessage - { - get => GetProperty("Shadow of the empire"); - set => SetProperty(value); - } - - public ICommand CloseCommand { get; } - - public ICommand OpenViewModelCommand { get; } - - public ICommand SaveViewModelCommand { get; } - - public ICommand ResetViewModelCommand { get; } - - public ThemeMode ThemeMode - { - get => GetProperty(ThemeMode.UseSystemSetting); - set => SetProperty(value, SetTheme); - } - - public AccentColors AccentColors - { - get => GetProperty(AccentColors.Green); - set => SetProperty(value, SetAccentColor); - } - - private static void SetAccentColor(AccentColors accentColors) => Current.AccentColor = accentColors.ToWindowsColor(); - - public int WindowWidth - { - get => GetProperty(1200); - set => SetProperty(value); - } - - public int WindowHeight - { - get => GetProperty(600); - set => SetProperty(value); - } - - public bool IsPaneOpen - { - get => GetProperty(true); - set => SetProperty(value); - } - - private static void SetTheme(ThemeMode themeMode) => Current.ApplicationTheme = themeMode.ToApplicationTheme(); - - private void ShowDialog() + [Command] + private async void ShowDialog() { var dialog = new ContentDialogExample {Message = WelcomeMessage}; - dialog.ShowAsync().Await(completedCallback: x => WelcomeMessage = x.ToString()); + var result = await dialog.ShowAsync(); + WelcomeMessage = result.ToString(); } - private void BooleanValue_OnChanged(bool obj) + private void BooleanValue_OnChanged() { - VisibilityEnumTest = obj ? Visibility.Visible : Visibility.Collapsed; + VisibilityEnumTest = BooleanValue ? Visibility.Visible : Visibility.Collapsed; } + [Command] private void SaveViewModel() { - var contents = _serializer.Serialize(Values); - if (Path is null) - { - var saveFileDialog = new SaveFileDialog {AddExtension = true, DefaultExt = "*.yaml"}; - var result = saveFileDialog.ShowDialog(Application.Current.MainWindow?.Owner); - if (result != true) return; - Path = saveFileDialog.FileName; - } - - File.WriteAllText(Path, contents); + // var contents = _serializer.Serialize(Values); + // if (Path is null) + // { + // var saveFileDialog = new SaveFileDialog {AddExtension = true, DefaultExt = "*.yaml"}; + // 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 = "*.yaml"}; - var result = openFileDialog.ShowDialog(Application.Current.MainWindow?.Owner); - if (result != true) yield break; - - var contents = File.ReadAllText(Path = openFileDialog.FileName); - - var obj = _deserializer.Deserialize>(contents); - foreach (var (key, value) in obj) - { - yield return (key, DeserializationExtension.Convert(value, ObjectAccessor[key].GetType())); - } - } } } \ No newline at end of file diff --git a/App/ModernWpfPlayground.csproj b/App/ModernWpfPlayground.csproj index e88e79c..54f5e04 100644 --- a/App/ModernWpfPlayground.csproj +++ b/App/ModernWpfPlayground.csproj @@ -5,15 +5,19 @@ net5.0-windows10.0.18362.0 true enable + true + true + win-x64 + true + true - - - - - + + + + diff --git a/App/MvvmStuff/BaseViewModel.cs b/App/MvvmStuff/BaseViewModel.cs deleted file mode 100644 index 7f248d9..0000000 --- a/App/MvvmStuff/BaseViewModel.cs +++ /dev/null @@ -1,67 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Runtime.CompilerServices; -using FastMember; -using Prism.Mvvm; - -namespace ModernWpfPlayground.MvvmStuff -{ - public abstract class BaseViewModel : BindableBase - { - protected BaseViewModel() - { - ObjectAccessor = ObjectAccessor.Create(this); - } - - private readonly Dictionary _values = new(); - protected readonly ObjectAccessor ObjectAccessor; - private readonly Dictionary _defaultValues = new(); - - protected IReadOnlyDictionary Values => _values; - - protected bool SetProperty(T value, Action? onChanged = null, - [CallerMemberName] string? propertyName = null) - { - if (propertyName == null) throw new ArgumentNullException(nameof(propertyName)); - if (_values.TryGetValue(propertyName, out var obj) && Equals(value, obj)) return false; - - _values[propertyName] = value; - RaisePropertyChanged(propertyName); - onChanged?.Invoke(value); - return true; - } - - protected T? GetProperty(T? defaultValue = default, [CallerMemberName] string? propertyName = null) - { - if (propertyName == null) throw new ArgumentNullException(nameof(propertyName)); - if (Values.TryGetValue(propertyName, out var obj)) - { - return (T) obj!; - } - - _defaultValues[propertyName] =defaultValue; - return defaultValue; - } - - protected void ResetViewModel() - { - foreach (var (key, value) in _values.ToArray()) - { - if (_defaultValues.TryGetValue(key, out var defaultValue) && Equals(value, defaultValue)) continue; - ObjectAccessor[key] = defaultValue; - } - } - - protected abstract IEnumerable<(string key, object? value)> GetViewModelItems(); - - protected void LoadViewModel() - { - ResetViewModel(); - foreach (var (key, value) in GetViewModelItems()) - { - ObjectAccessor[key] = value; - } - } - } -} \ No newline at end of file diff --git a/Controls/Controls.csproj b/Controls/Controls.csproj index f6df3a4..b6510d6 100644 --- a/Controls/Controls.csproj +++ b/Controls/Controls.csproj @@ -7,7 +7,7 @@ - +