Wire up settings

This commit is contained in:
TheXamlGuy
2024-02-05 22:01:05 +00:00
parent 43f96fd4f0
commit 28d79f77d0
7 changed files with 62 additions and 22 deletions
+5
View File
@@ -10,6 +10,11 @@ public class WindowHandler :
{ {
if (args.Template is Window window) if (args.Template is Window window)
{ {
if (window.Content is FrameworkElement frameworkElement)
{
frameworkElement.DataContext = args.Content;
}
window.Activate(); window.Activate();
} }
+1 -1
View File
@@ -53,7 +53,7 @@ public partial class App :
services.AddContentTemplate<SecondaryViewModel, SecondaryView>(); services.AddContentTemplate<SecondaryViewModel, SecondaryView>();
services.AddContentTemplate<SettingsButtonViewModel, SettingsButtonView>(); services.AddContentTemplate<SettingsButtonViewModel, SettingsButtonView>();
services.AddContentTemplate<SettingsView, SettingsView>("Settings"); services.AddContentTemplate<SettingsViewModel, SettingsView>("Settings");
services.AddTransient<IInitializer, AppInitializer>(); services.AddTransient<IInitializer, AppInitializer>();
}) })
+16 -2
View File
@@ -2,6 +2,20 @@
<Window <Window
x:Class="Hyperbar.Windows.SettingsView" x:Class="Hyperbar.Windows.SettingsView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
<Button>afsdfdg</Button> xmlns:hyperbar="using:Hyperbar">
<Window.SystemBackdrop>
<MicaBackdrop />
</Window.SystemBackdrop>
<NavigationView
IsBackButtonVisible="Collapsed"
IsPaneToggleButtonVisible="False"
IsSettingsVisible="False"
MenuItemsSource="{x:Bind ViewModel, Mode=OneWay}">
<NavigationView.MenuItemTemplate>
<DataTemplate x:DataType="hyperbar:NavigationViewModel">
<NavigationViewItem Content="{x:Bind Text, Mode=OneWay}" />
</DataTemplate>
</NavigationView.MenuItemTemplate>
</NavigationView>
</Window> </Window>
+7 -6
View File
@@ -1,12 +1,13 @@
using Microsoft.UI.Xaml; using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
namespace Hyperbar.Windows namespace Hyperbar.Windows;
{
public sealed partial class SettingsView : public partial class SettingsView :
Window Window
{ {
public SettingsView() => public SettingsView() =>
InitializeComponent(); InitializeComponent();
}
protected SettingsViewModel ViewModel =>
(SettingsViewModel)(Content as FrameworkElement)!.DataContext;
} }
+8 -7
View File
@@ -1,12 +1,13 @@
using System; namespace Hyperbar.Windows;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Hyperbar.Windows public partial class SettingsViewModel :
ObservableCollectionViewModel<NavigationViewModel>
{ {
internal class SettingsViewModel public SettingsViewModel(IServiceFactory serviceFactory,
IMediator mediator,
IDisposer disposer) : base(serviceFactory, mediator, disposer)
{ {
Add<NavigationViewModel>("General");
Add<NavigationViewModel>("Widgets");
} }
} }
+4 -3
View File
@@ -34,13 +34,14 @@ public class NavigateHandler :
contentTemplateDescriptor.TemplateType.BaseType == navigationDescriptor.Type) contentTemplateDescriptor.TemplateType.BaseType == navigationDescriptor.Type)
{ {
if (provider.GetRequiredKeyedService(contentTemplateDescriptor.TemplateType, 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<>) Type navigateType = typeof(Navigate<>)
.MakeGenericType(navigationDescriptor.Type); .MakeGenericType(navigationDescriptor.Type);
if (Activator.CreateInstance(navigateType, if (Activator.CreateInstance(navigateType,
new object[] { template, args.Key }) is object navigate) new object[] { template, content }) is object navigate)
{ {
await mediator.PublishAsync(navigate, cancellationToken); await mediator.PublishAsync(navigate, cancellationToken);
} }
@@ -0,0 +1,18 @@
using CommunityToolkit.Mvvm.ComponentModel;
namespace Hyperbar;
public partial class NavigationViewModel :
ObservableCollectionViewModel<NavigationViewModel>
{
[ObservableProperty]
private string? text;
public NavigationViewModel(IServiceFactory serviceFactory,
IMediator mediator,
IDisposer disposer,
string text) : base(serviceFactory, mediator, disposer)
{
this.text = text;
}
}