From 28d79f77d05b01545a7ccda7c0ed91019dcfa91d Mon Sep 17 00:00:00 2001 From: TheXamlGuy Date: Mon, 5 Feb 2024 22:01:05 +0000 Subject: [PATCH] Wire up settings --- Hyperbar.UI.Windows/WindowHandler.cs | 5 +++++ Hyperbar.Windows/App.xaml.cs | 2 +- Hyperbar.Windows/SettingsView.xaml | 18 ++++++++++++++++-- Hyperbar.Windows/SettingsView.xaml.cs | 17 +++++++++-------- Hyperbar.Windows/SettingsViewModel.cs | 15 ++++++++------- Hyperbar/Lifecycles/NavigateHandler.cs | 9 +++++---- Hyperbar/Lifecycles/NavigationViewModel.cs | 18 ++++++++++++++++++ 7 files changed, 62 insertions(+), 22 deletions(-) create mode 100644 Hyperbar/Lifecycles/NavigationViewModel.cs diff --git a/Hyperbar.UI.Windows/WindowHandler.cs b/Hyperbar.UI.Windows/WindowHandler.cs index 9919f0f..46f3054 100644 --- a/Hyperbar.UI.Windows/WindowHandler.cs +++ b/Hyperbar.UI.Windows/WindowHandler.cs @@ -10,6 +10,11 @@ public class WindowHandler : { if (args.Template is Window window) { + if (window.Content is FrameworkElement frameworkElement) + { + frameworkElement.DataContext = args.Content; + } + window.Activate(); } diff --git a/Hyperbar.Windows/App.xaml.cs b/Hyperbar.Windows/App.xaml.cs index 4c38eb6..a7ffcc3 100644 --- a/Hyperbar.Windows/App.xaml.cs +++ b/Hyperbar.Windows/App.xaml.cs @@ -53,7 +53,7 @@ public partial class App : services.AddContentTemplate(); services.AddContentTemplate(); - services.AddContentTemplate("Settings"); + services.AddContentTemplate("Settings"); services.AddTransient(); }) diff --git a/Hyperbar.Windows/SettingsView.xaml b/Hyperbar.Windows/SettingsView.xaml index faf18d4..ea28db8 100644 --- a/Hyperbar.Windows/SettingsView.xaml +++ b/Hyperbar.Windows/SettingsView.xaml @@ -2,6 +2,20 @@ - + xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" + xmlns:hyperbar="using:Hyperbar"> + + + + + + + + + + diff --git a/Hyperbar.Windows/SettingsView.xaml.cs b/Hyperbar.Windows/SettingsView.xaml.cs index d91e98f..e064723 100644 --- a/Hyperbar.Windows/SettingsView.xaml.cs +++ b/Hyperbar.Windows/SettingsView.xaml.cs @@ -1,12 +1,13 @@ using Microsoft.UI.Xaml; -using Microsoft.UI.Xaml.Controls; -namespace Hyperbar.Windows +namespace Hyperbar.Windows; + +public partial class SettingsView : + Window { - public sealed partial class SettingsView : - Window - { - public SettingsView() => - InitializeComponent(); - } + public SettingsView() => + InitializeComponent(); + + protected SettingsViewModel ViewModel => + (SettingsViewModel)(Content as FrameworkElement)!.DataContext; } diff --git a/Hyperbar.Windows/SettingsViewModel.cs b/Hyperbar.Windows/SettingsViewModel.cs index e2511ee..393523b 100644 --- a/Hyperbar.Windows/SettingsViewModel.cs +++ b/Hyperbar.Windows/SettingsViewModel.cs @@ -1,12 +1,13 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +namespace Hyperbar.Windows; -namespace Hyperbar.Windows +public partial class SettingsViewModel : + ObservableCollectionViewModel { - internal class SettingsViewModel + public SettingsViewModel(IServiceFactory serviceFactory, + IMediator mediator, + IDisposer disposer) : base(serviceFactory, mediator, disposer) { + Add("General"); + Add("Widgets"); } } diff --git a/Hyperbar/Lifecycles/NavigateHandler.cs b/Hyperbar/Lifecycles/NavigateHandler.cs index dfc0c9e..9732260 100644 --- a/Hyperbar/Lifecycles/NavigateHandler.cs +++ b/Hyperbar/Lifecycles/NavigateHandler.cs @@ -34,13 +34,14 @@ public class NavigateHandler : contentTemplateDescriptor.TemplateType.BaseType == navigationDescriptor.Type) { if (provider.GetRequiredKeyedService(contentTemplateDescriptor.TemplateType, - contentTemplateDescriptor.Key) is { } template) + contentTemplateDescriptor.Key) is { } template && + provider.GetRequiredKeyedService(contentTemplateDescriptor.ContentType, + contentTemplateDescriptor.Key) is { } content) { Type navigateType = typeof(Navigate<>) .MakeGenericType(navigationDescriptor.Type); - - if (Activator.CreateInstance(navigateType, - new object[] { template, args.Key }) is object navigate) + if (Activator.CreateInstance(navigateType, + new object[] { template, content }) is object navigate) { await mediator.PublishAsync(navigate, cancellationToken); } diff --git a/Hyperbar/Lifecycles/NavigationViewModel.cs b/Hyperbar/Lifecycles/NavigationViewModel.cs new file mode 100644 index 0000000..415fc79 --- /dev/null +++ b/Hyperbar/Lifecycles/NavigationViewModel.cs @@ -0,0 +1,18 @@ +using CommunityToolkit.Mvvm.ComponentModel; + +namespace Hyperbar; + +public partial class NavigationViewModel : + ObservableCollectionViewModel +{ + [ObservableProperty] + private string? text; + + public NavigationViewModel(IServiceFactory serviceFactory, + IMediator mediator, + IDisposer disposer, + string text) : base(serviceFactory, mediator, disposer) + { + this.text = text; + } +}