From 31d47a2340173b09ec30b1d1e6ab201796204fc1 Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Wed, 5 Mar 2025 15:18:06 +0800 Subject: [PATCH] Use dependency injection instead of navigation parameter --- .../SettingPages/Views/SettingsPaneAbout.xaml.cs | 10 ++++++---- .../SettingPages/Views/SettingsPaneGeneral.xaml.cs | 11 +++++++---- .../SettingPages/Views/SettingsPaneHotkey.xaml.cs | 8 ++++---- .../Views/SettingsPanePluginStore.xaml.cs | 8 ++++---- .../SettingPages/Views/SettingsPanePlugins.xaml.cs | 8 ++++---- .../SettingPages/Views/SettingsPaneProxy.xaml.cs | 10 ++++++---- .../SettingPages/Views/SettingsPaneTheme.xaml.cs | 10 ++++------ Flow.Launcher/SettingWindow.xaml.cs | 13 ++----------- 8 files changed, 37 insertions(+), 41 deletions(-) diff --git a/Flow.Launcher/SettingPages/Views/SettingsPaneAbout.xaml.cs b/Flow.Launcher/SettingPages/Views/SettingsPaneAbout.xaml.cs index de6a4df5ee8..1ecc02aa6b3 100644 --- a/Flow.Launcher/SettingPages/Views/SettingsPaneAbout.xaml.cs +++ b/Flow.Launcher/SettingPages/Views/SettingsPaneAbout.xaml.cs @@ -1,6 +1,8 @@ -using System; -using System.Windows.Navigation; +using System.Windows.Navigation; +using Flow.Launcher.Core; using Flow.Launcher.SettingPages.ViewModels; +using Flow.Launcher.Infrastructure.UserSettings; +using CommunityToolkit.Mvvm.DependencyInjection; namespace Flow.Launcher.SettingPages.Views; @@ -12,8 +14,8 @@ protected override void OnNavigatedTo(NavigationEventArgs e) { if (!IsInitialized) { - if (e.ExtraData is not SettingWindow.PaneData { Settings: { } settings, Updater: { } updater }) - throw new ArgumentException("Settings are required for SettingsPaneAbout."); + var settings = Ioc.Default.GetRequiredService(); + var updater = Ioc.Default.GetRequiredService(); _viewModel = new SettingsPaneAboutViewModel(settings, updater); DataContext = _viewModel; InitializeComponent(); diff --git a/Flow.Launcher/SettingPages/Views/SettingsPaneGeneral.xaml.cs b/Flow.Launcher/SettingPages/Views/SettingsPaneGeneral.xaml.cs index f95015b2eef..dd7fd13a995 100644 --- a/Flow.Launcher/SettingPages/Views/SettingsPaneGeneral.xaml.cs +++ b/Flow.Launcher/SettingPages/Views/SettingsPaneGeneral.xaml.cs @@ -1,5 +1,7 @@ -using System; -using System.Windows.Navigation; +using System.Windows.Navigation; +using CommunityToolkit.Mvvm.DependencyInjection; +using Flow.Launcher.Core; +using Flow.Launcher.Core.Configuration; using Flow.Launcher.Infrastructure.UserSettings; using Flow.Launcher.SettingPages.ViewModels; @@ -13,8 +15,9 @@ protected override void OnNavigatedTo(NavigationEventArgs e) { if (!IsInitialized) { - if (e.ExtraData is not SettingWindow.PaneData { Settings: { } settings, Updater: {} updater, Portable: {} portable }) - throw new ArgumentException("Settings, Updater and Portable are required for SettingsPaneGeneral."); + var settings = Ioc.Default.GetRequiredService(); + var updater = Ioc.Default.GetRequiredService(); + var portable = Ioc.Default.GetRequiredService(); _viewModel = new SettingsPaneGeneralViewModel(settings, updater, portable); DataContext = _viewModel; InitializeComponent(); diff --git a/Flow.Launcher/SettingPages/Views/SettingsPaneHotkey.xaml.cs b/Flow.Launcher/SettingPages/Views/SettingsPaneHotkey.xaml.cs index 061eabf515d..eb100da0ca8 100644 --- a/Flow.Launcher/SettingPages/Views/SettingsPaneHotkey.xaml.cs +++ b/Flow.Launcher/SettingPages/Views/SettingsPaneHotkey.xaml.cs @@ -1,6 +1,7 @@ -using System; -using System.Windows.Navigation; +using System.Windows.Navigation; +using CommunityToolkit.Mvvm.DependencyInjection; using Flow.Launcher.SettingPages.ViewModels; +using Flow.Launcher.Infrastructure.UserSettings; namespace Flow.Launcher.SettingPages.Views; @@ -12,8 +13,7 @@ protected override void OnNavigatedTo(NavigationEventArgs e) { if (!IsInitialized) { - if (e.ExtraData is not SettingWindow.PaneData { Settings: { } settings }) - throw new ArgumentException("Settings are required for SettingsPaneHotkey."); + var settings = Ioc.Default.GetRequiredService(); _viewModel = new SettingsPaneHotkeyViewModel(settings); DataContext = _viewModel; InitializeComponent(); diff --git a/Flow.Launcher/SettingPages/Views/SettingsPanePluginStore.xaml.cs b/Flow.Launcher/SettingPages/Views/SettingsPanePluginStore.xaml.cs index db476331908..3bd24bc1323 100644 --- a/Flow.Launcher/SettingPages/Views/SettingsPanePluginStore.xaml.cs +++ b/Flow.Launcher/SettingPages/Views/SettingsPanePluginStore.xaml.cs @@ -1,10 +1,11 @@ -using System; -using System.ComponentModel; +using System.ComponentModel; using System.Windows.Data; using System.Windows.Input; using System.Windows.Navigation; +using CommunityToolkit.Mvvm.DependencyInjection; using Flow.Launcher.SettingPages.ViewModels; using Flow.Launcher.ViewModel; +using Flow.Launcher.Infrastructure.UserSettings; namespace Flow.Launcher.SettingPages.Views; @@ -16,8 +17,7 @@ protected override void OnNavigatedTo(NavigationEventArgs e) { if (!IsInitialized) { - if (e.ExtraData is not SettingWindow.PaneData { Settings: { } settings }) - throw new ArgumentException($"Settings are required for {nameof(SettingsPanePluginStore)}."); + var settings = Ioc.Default.GetRequiredService(); _viewModel = new SettingsPanePluginStoreViewModel(); DataContext = _viewModel; InitializeComponent(); diff --git a/Flow.Launcher/SettingPages/Views/SettingsPanePlugins.xaml.cs b/Flow.Launcher/SettingPages/Views/SettingsPanePlugins.xaml.cs index d48505c3dbe..f6b4351864e 100644 --- a/Flow.Launcher/SettingPages/Views/SettingsPanePlugins.xaml.cs +++ b/Flow.Launcher/SettingPages/Views/SettingsPanePlugins.xaml.cs @@ -1,7 +1,8 @@ -using System; -using System.Windows.Input; +using System.Windows.Input; using System.Windows.Navigation; +using CommunityToolkit.Mvvm.DependencyInjection; using Flow.Launcher.SettingPages.ViewModels; +using Flow.Launcher.Infrastructure.UserSettings; namespace Flow.Launcher.SettingPages.Views; @@ -13,8 +14,7 @@ protected override void OnNavigatedTo(NavigationEventArgs e) { if (!IsInitialized) { - if (e.ExtraData is not SettingWindow.PaneData { Settings: { } settings }) - throw new ArgumentException("Settings are required for SettingsPaneHotkey."); + var settings = Ioc.Default.GetRequiredService(); _viewModel = new SettingsPanePluginsViewModel(settings); DataContext = _viewModel; InitializeComponent(); diff --git a/Flow.Launcher/SettingPages/Views/SettingsPaneProxy.xaml.cs b/Flow.Launcher/SettingPages/Views/SettingsPaneProxy.xaml.cs index 95c88d6277b..26350b8bbd5 100644 --- a/Flow.Launcher/SettingPages/Views/SettingsPaneProxy.xaml.cs +++ b/Flow.Launcher/SettingPages/Views/SettingsPaneProxy.xaml.cs @@ -1,6 +1,8 @@ -using System; -using System.Windows.Navigation; +using System.Windows.Navigation; +using CommunityToolkit.Mvvm.DependencyInjection; +using Flow.Launcher.Core; using Flow.Launcher.SettingPages.ViewModels; +using Flow.Launcher.Infrastructure.UserSettings; namespace Flow.Launcher.SettingPages.Views; @@ -12,8 +14,8 @@ protected override void OnNavigatedTo(NavigationEventArgs e) { if (!IsInitialized) { - if (e.ExtraData is not SettingWindow.PaneData { Settings: { } settings, Updater: { } updater }) - throw new ArgumentException($"Settings are required for {nameof(SettingsPaneProxy)}."); + var settings = Ioc.Default.GetRequiredService(); + var updater = Ioc.Default.GetRequiredService(); _viewModel = new SettingsPaneProxyViewModel(settings, updater); DataContext = _viewModel; InitializeComponent(); diff --git a/Flow.Launcher/SettingPages/Views/SettingsPaneTheme.xaml.cs b/Flow.Launcher/SettingPages/Views/SettingsPaneTheme.xaml.cs index 7f32728c217..bf7502e1927 100644 --- a/Flow.Launcher/SettingPages/Views/SettingsPaneTheme.xaml.cs +++ b/Flow.Launcher/SettingPages/Views/SettingsPaneTheme.xaml.cs @@ -1,10 +1,9 @@ -using System; -using System.Windows; -using System.Windows.Controls; -using System.Windows.Media; +using System.Windows.Controls; using System.Windows.Navigation; +using CommunityToolkit.Mvvm.DependencyInjection; using Flow.Launcher.SettingPages.ViewModels; using Page = ModernWpf.Controls.Page; +using Flow.Launcher.Infrastructure.UserSettings; namespace Flow.Launcher.SettingPages.Views; @@ -16,8 +15,7 @@ protected override void OnNavigatedTo(NavigationEventArgs e) { if (!IsInitialized) { - if (e.ExtraData is not SettingWindow.PaneData { Settings: { } settings }) - throw new ArgumentException($"Settings are required for {nameof(SettingsPaneTheme)}."); + var settings = Ioc.Default.GetRequiredService(); _viewModel = new SettingsPaneThemeViewModel(settings); DataContext = _viewModel; InitializeComponent(); diff --git a/Flow.Launcher/SettingWindow.xaml.cs b/Flow.Launcher/SettingWindow.xaml.cs index f87194c30cb..30b51f992b0 100644 --- a/Flow.Launcher/SettingWindow.xaml.cs +++ b/Flow.Launcher/SettingWindow.xaml.cs @@ -4,8 +4,6 @@ using System.Windows.Input; using System.Windows.Interop; using CommunityToolkit.Mvvm.DependencyInjection; -using Flow.Launcher.Core; -using Flow.Launcher.Core.Configuration; using Flow.Launcher.Helper; using Flow.Launcher.Infrastructure.UserSettings; using Flow.Launcher.Plugin; @@ -18,8 +16,6 @@ namespace Flow.Launcher; public partial class SettingWindow { - private readonly Updater _updater; - private readonly IPortable _portable; private readonly IPublicAPI _api; private readonly Settings _settings; private readonly SettingWindowViewModel _viewModel; @@ -30,8 +26,6 @@ public SettingWindow() _settings = Ioc.Default.GetRequiredService(); DataContext = viewModel; _viewModel = viewModel; - _updater = Ioc.Default.GetRequiredService(); - _portable = Ioc.Default.GetRequiredService(); _api = Ioc.Default.GetRequiredService(); InitializePosition(); InitializeComponent(); @@ -166,10 +160,9 @@ private double WindowTop() private void NavigationView_SelectionChanged(NavigationView sender, NavigationViewSelectionChangedEventArgs args) { - var paneData = new PaneData(_settings, _updater, _portable); if (args.IsSettingsSelected) { - ContentFrame.Navigate(typeof(SettingsPaneGeneral), paneData); + ContentFrame.Navigate(typeof(SettingsPaneGeneral)); } else { @@ -191,7 +184,7 @@ private void NavigationView_SelectionChanged(NavigationView sender, NavigationVi nameof(About) => typeof(SettingsPaneAbout), _ => typeof(SettingsPaneGeneral) }; - ContentFrame.Navigate(pageType, paneData); + ContentFrame.Navigate(pageType); } } @@ -211,6 +204,4 @@ private void ContentFrame_Loaded(object sender, RoutedEventArgs e) { NavView.SelectedItem ??= NavView.MenuItems[0]; /* Set First Page */ } - - public record PaneData(Settings Settings, Updater Updater, IPortable Portable); }