wpfui 4.0.0 (#1255)

* new INavigationAware, remove PageService

* fix run

* fix title bar

* override OnNavigatedTo

* try fix WindowStartupLocation
This commit is contained in:
辉鸭蛋
2025-03-13 00:33:31 +08:00
committed by GitHub
parent 6becb9dd5a
commit 46ebdfc452
18 changed files with 117 additions and 157 deletions

View File

@@ -11,7 +11,6 @@ using BetterGenshinImpact.View.Pages;
using BetterGenshinImpact.ViewModel;
using BetterGenshinImpact.ViewModel.Pages;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Serilog;
@@ -22,10 +21,9 @@ using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using System.Windows;
using BetterGenshinImpact.View.Pages.View;
using BetterGenshinImpact.ViewModel.Pages.OneDragon;
using BetterGenshinImpact.ViewModel.Pages.View;
using Wpf.Ui;
using Wpf.Ui.DependencyInjection;
using Wpf.Ui.Violeta.Controls;
namespace BetterGenshinImpact;
@@ -73,10 +71,11 @@ public partial class App : Application
Log.Logger = loggerConfiguration.CreateLogger();
services.AddLogging(c => c.AddSerilog());
services.AddNavigationViewPageProvider();
// App Host
services.AddHostedService<ApplicationHostService>();
// Page resolver service
services.AddSingleton<IPageService, PageService>();
services.AddSingleton<INavigationService, NavigationService>();
services.AddSingleton<IUpdateService, UpdateService>();
// Service containing navigation, same as INavigationWindow... but without window

View File

@@ -75,9 +75,10 @@
<PackageReference Include="Vanara.PInvoke.NtDll" Version="4.0.2" />
<PackageReference Include="Vanara.PInvoke.SHCore" Version="4.0.2" />
<PackageReference Include="Vanara.PInvoke.User32" Version="4.0.2" />
<PackageReference Include="WPF-UI" Version="3.0.5" />
<PackageReference Include="WPF-UI.Tray" Version="3.0.5" />
<PackageReference Include="WPF-UI.Violeta" Version="3.0.5.26" />
<PackageReference Include="WPF-UI" Version="4.0.0" />
<PackageReference Include="WPF-UI.DependencyInjection" Version="4.0.0" />
<PackageReference Include="WPF-UI.Tray" Version="4.0.0" />
<PackageReference Include="WPF-UI.Violeta" Version="3.0.5.28" />
<PackageReference Include="YoloV8" Version="4.1.7" />
<PackageReference Include="gong-wpf-dragdrop" Version="3.2.1" />
</ItemGroup>

View File

@@ -13,16 +13,10 @@ namespace BetterGenshinImpact.Service;
/// <summary>
/// Managed host of the application.
/// </summary>
public class ApplicationHostService : IHostedService
public class ApplicationHostService(IServiceProvider serviceProvider) : IHostedService
{
private readonly IServiceProvider _serviceProvider;
private INavigationWindow? _navigationWindow;
public ApplicationHostService(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}
/// <summary>
/// Triggered when the application host is ready to start the service.
/// </summary>
@@ -50,12 +44,10 @@ public class ApplicationHostService : IHostedService
if (!Application.Current.Windows.OfType<MainWindow>().Any())
{
_navigationWindow = (
_serviceProvider.GetService(typeof(INavigationWindow)) as INavigationWindow
)!;
_navigationWindow = (serviceProvider.GetService(typeof(INavigationWindow)) as INavigationWindow)!;
_navigationWindow!.ShowWindow();
_navigationWindow.Navigate(typeof(HomePage));
_ = _navigationWindow.Navigate(typeof(HomePage));
}
await Task.CompletedTask;

View File

@@ -1,46 +1,46 @@
using System;
using System.Windows;
using Wpf.Ui;
namespace BetterGenshinImpact.Service;
/// <summary>
/// Service that provides pages for navigation.
/// </summary>
public class PageService : IPageService
{
/// <summary>
/// Service which provides the instances of pages.
/// </summary>
private readonly IServiceProvider _serviceProvider;
/// <summary>
/// Creates new instance and attaches the <see cref="IServiceProvider"/>.
/// </summary>
public PageService(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}
/// <inheritdoc />
public T? GetPage<T>() where T : class
{
if (!typeof(FrameworkElement).IsAssignableFrom(typeof(T)))
{
throw new InvalidOperationException("The page should be a WPF control.");
}
return (T?)_serviceProvider.GetService(typeof(T));
}
/// <inheritdoc />
public FrameworkElement? GetPage(Type pageType)
{
if (!typeof(FrameworkElement).IsAssignableFrom(pageType))
{
throw new InvalidOperationException("The page should be a WPF control.");
}
return _serviceProvider.GetService(pageType) as FrameworkElement;
}
}
// using System;
// using System.Windows;
// using Wpf.Ui;
//
// namespace BetterGenshinImpact.Service;
//
// /// <summary>
// /// Service that provides pages for navigation.
// /// </summary>
// public class PageService : IPageService
// {
// /// <summary>
// /// Service which provides the instances of pages.
// /// </summary>
// private readonly IServiceProvider _serviceProvider;
//
// /// <summary>
// /// Creates new instance and attaches the <see cref="IServiceProvider"/>.
// /// </summary>
// public PageService(IServiceProvider serviceProvider)
// {
// _serviceProvider = serviceProvider;
// }
//
// /// <inheritdoc />
// public T? GetPage<T>() where T : class
// {
// if (!typeof(FrameworkElement).IsAssignableFrom(typeof(T)))
// {
// throw new InvalidOperationException("The page should be a WPF control.");
// }
//
// return (T?)_serviceProvider.GetService(typeof(T));
// }
//
// /// <inheritdoc />
// public FrameworkElement? GetPage(Type pageType)
// {
// if (!typeof(FrameworkElement).IsAssignableFrom(pageType))
// {
// throw new InvalidOperationException("The page should be a WPF control.");
// }
//
// return _serviceProvider.GetService(pageType) as FrameworkElement;
// }
// }

View File

@@ -177,7 +177,7 @@
<ui:TitleBar.Icon>
<ui:ImageIcon Source="pack://application:,,,/Assets/Images/logo.png" />
</ui:TitleBar.Icon>
<ui:TitleBar.Header>
<ui:TitleBar.TrailingContent>
<StackPanel Orientation="Horizontal" VerticalAlignment="Top">
<ui:Button Width="46"
Height="32"
@@ -197,7 +197,7 @@
Icon="{ui:SymbolIcon CaretDown24}"
ToolTip="最小化到托盘" />
</StackPanel>
</ui:TitleBar.Header>
</ui:TitleBar.TrailingContent>
</ui:TitleBar>
<tray:NotifyIcon Grid.Row="0"

View File

@@ -7,18 +7,19 @@ using System.Windows.Media;
using BetterGenshinImpact.GameTask;
using BetterGenshinImpact.Helpers.Ui;
using Wpf.Ui;
using Wpf.Ui.Abstractions;
using Wpf.Ui.Controls;
using Wpf.Ui.Tray.Controls;
namespace BetterGenshinImpact.View;
public partial class MainWindow : FluentWindow, INavigationWindow
public partial class MainWindow : INavigationWindow
{
private readonly ILogger<MainWindow> _logger = App.GetLogger<MainWindow>();
public MainWindowViewModel ViewModel { get; }
public MainWindow(MainWindowViewModel viewModel, IPageService pageService, INavigationService navigationService, ISnackbarService snackbarService)
public MainWindow(MainWindowViewModel viewModel, INavigationService navigationService, ISnackbarService snackbarService)
{
_logger.LogDebug("主窗体实例化");
DataContext = ViewModel = viewModel;
@@ -26,7 +27,6 @@ public partial class MainWindow : FluentWindow, INavigationWindow
InitializeComponent();
this.InitializeDpiAwareness();
SetPageService(pageService);
snackbarService.SetSnackbarPresenter(SnackbarPresenter);
navigationService.SetNavigationControl(RootNavigation);
@@ -62,10 +62,8 @@ public partial class MainWindow : FluentWindow, INavigationWindow
throw new NotImplementedException();
}
public void SetPageService(IPageService pageService)
{
RootNavigation.SetPageService(pageService);
}
public void SetPageService(INavigationViewPageProvider navigationViewPageProvider) =>
RootNavigation.SetPageProviderService(navigationViewPageProvider);
public void ShowWindow() => Show();

View File

@@ -27,7 +27,7 @@ using Wpf.Ui.Violeta.Controls;
namespace BetterGenshinImpact.ViewModel.Pages;
public partial class CommonSettingsPageViewModel : ObservableObject, INavigationAware, IViewModel
public partial class CommonSettingsPageViewModel : ViewModel
{
public AllConfig Config { get; set; }
@@ -136,14 +136,6 @@ public partial class CommonSettingsPageViewModel : ObservableObject, INavigation
InitializeCountries();
}
public void OnNavigatedTo()
{
}
public void OnNavigatedFrom()
{
}
[RelayCommand]
public void OnRefreshMaskSettings()
{

View File

@@ -27,7 +27,7 @@ using Wpf.Ui.Controls;
namespace BetterGenshinImpact.ViewModel.Pages;
public partial class HomePageViewModel : ObservableObject, INavigationAware, IViewModel
public partial class HomePageViewModel : ViewModel
{
[ObservableProperty]
private string[] _modeNames = GameCaptureFactory.ModeNames();
@@ -293,14 +293,6 @@ public partial class HomePageViewModel : ObservableObject, INavigationAware, IVi
UIDispatcherHelper.Invoke(() => Start(_hWnd));
}
public void OnNavigatedTo()
{
}
public void OnNavigatedFrom()
{
}
[RelayCommand]
public void OnGoToWikiUrl()
{

View File

@@ -21,7 +21,7 @@ using Wpf.Ui.Violeta.Controls;
namespace BetterGenshinImpact.ViewModel.Pages;
public partial class JsListViewModel : ObservableObject, INavigationAware, IViewModel
public partial class JsListViewModel : ViewModel
{
private readonly ILogger<JsListViewModel> _logger = App.GetLogger<JsListViewModel>();
private readonly string scriptPath = Global.ScriptPath();
@@ -71,15 +71,11 @@ public partial class JsListViewModel : ObservableObject, INavigationAware, IView
return di.GetDirectories();
}
public void OnNavigatedTo()
public override void OnNavigatedTo()
{
InitScriptListViewData();
}
public void OnNavigatedFrom()
{
}
[RelayCommand]
public void OnOpenScriptsFolder()
{

View File

@@ -18,7 +18,7 @@ using static Vanara.PInvoke.User32;
namespace BetterGenshinImpact.ViewModel.Pages;
public partial class KeyBindingsSettingsPageViewModel : ObservableObject, INavigationAware, IViewModel
public partial class KeyBindingsSettingsPageViewModel : ViewModel
{
private readonly ILogger<KeyBindingsSettingsPageViewModel> _logger;
@@ -30,14 +30,6 @@ public partial class KeyBindingsSettingsPageViewModel : ObservableObject, INavig
[ObservableProperty]
private ObservableCollection<KeyBindingSettingModel> _keyBindingSettingModels = [];
public void OnNavigatedFrom()
{
}
public void OnNavigatedTo()
{
}
public KeyBindingsSettingsPageViewModel(IConfigService configService, ILogger<KeyBindingsSettingsPageViewModel> logger)
{
_logger = logger;

View File

@@ -22,7 +22,7 @@ using Wpf.Ui.Violeta.Controls;
namespace BetterGenshinImpact.ViewModel.Pages;
public partial class KeyMouseRecordPageViewModel : ObservableObject, INavigationAware, IViewModel
public partial class KeyMouseRecordPageViewModel : ViewModel
{
private readonly ILogger<KeyMouseRecordPageViewModel> _logger = App.GetLogger<KeyMouseRecordPageViewModel>();
private readonly string scriptPath = Global.Absolute(@"User\KeyMouseScript");
@@ -74,15 +74,11 @@ public partial class KeyMouseRecordPageViewModel : ObservableObject, INavigation
return files.Select(file => new FileInfo(file)).ToList();
}
public void OnNavigatedTo()
public override void OnNavigatedTo()
{
InitScriptListViewData();
}
public void OnNavigatedFrom()
{
}
[RelayCommand]
public async Task OnStartRecord()
{

View File

@@ -11,7 +11,7 @@ using Wpf.Ui.Controls;
namespace BetterGenshinImpact.ViewModel.Pages;
public partial class MacroSettingsPageViewModel : ObservableObject, INavigationAware, IViewModel
public partial class MacroSettingsPageViewModel : ViewModel
{
public AllConfig Config { get; set; }
@@ -25,15 +25,7 @@ public partial class MacroSettingsPageViewModel : ObservableObject, INavigationA
Config = configService.Get();
_navigationService = navigationService;
}
public void OnNavigatedTo()
{
}
public void OnNavigatedFrom()
{
}
[RelayCommand]
public void OnGoToHotKeyPage()
{

View File

@@ -17,17 +17,14 @@ using System.Threading.Tasks;
using BetterGenshinImpact.Core.Script;
using BetterGenshinImpact.ViewModel.Message;
using CommunityToolkit.Mvvm.Messaging;
using Wpf.Ui.Controls;
using Wpf.Ui.Violeta.Controls;
using System.Windows;
using BetterGenshinImpact.GameTask;
using BetterGenshinImpact.View.Pages.View;
using BetterGenshinImpact.ViewModel.Pages.View;
using Wpf.Ui.Violeta.Win32;
namespace BetterGenshinImpact.ViewModel.Pages;
public partial class MapPathingViewModel : ObservableObject, INavigationAware, IViewModel
public partial class MapPathingViewModel : ViewModel
{
private readonly ILogger<MapPathingViewModel> _logger = App.GetLogger<MapPathingViewModel>();
public static readonly string PathJsonPath = Global.Absolute(@"User\AutoPathing");
@@ -74,15 +71,11 @@ public partial class MapPathingViewModel : ObservableObject, INavigationAware, I
}
}
public void OnNavigatedTo()
public override void OnNavigatedTo()
{
InitScriptListViewData();
}
public void OnNavigatedFrom()
{
}
[RelayCommand]
public void OnOpenScriptsFolder()
{

View File

@@ -28,7 +28,7 @@ using Wpf.Ui.Violeta.Controls;
namespace BetterGenshinImpact.ViewModel.Pages;
public partial class OneDragonFlowViewModel : ObservableObject, INavigationAware, IViewModel
public partial class OneDragonFlowViewModel : ViewModel
{
private readonly ILogger<OneDragonFlowViewModel> _logger = App.GetLogger<OneDragonFlowViewModel>();
@@ -97,7 +97,7 @@ public partial class OneDragonFlowViewModel : ObservableObject, INavigationAware
InitConfigList();
}
public void OnNavigatedTo()
public override void OnNavigatedTo()
{
InitConfigList();
}

View File

@@ -41,7 +41,7 @@ using TextBlock = Wpf.Ui.Controls.TextBlock;
namespace BetterGenshinImpact.ViewModel.Pages;
public partial class ScriptControlViewModel : ObservableObject, INavigationAware, IViewModel
public partial class ScriptControlViewModel : ViewModel
{
private readonly ISnackbarService _snackbarService;
@@ -63,12 +63,9 @@ public partial class ScriptControlViewModel : ObservableObject, INavigationAware
public readonly string ScriptGroupPath = Global.Absolute(@"User\ScriptGroup");
public readonly string LogPath = Global.Absolute(@"log");
public void OnNavigatedFrom()
{
}
public void OnNavigatedTo()
public override void OnNavigatedTo()
{
ReadScriptGroup();
}

View File

@@ -35,7 +35,7 @@ using Vanara.Extensions;
namespace BetterGenshinImpact.ViewModel.Pages;
public partial class TaskSettingsPageViewModel : ObservableObject, INavigationAware, IViewModel
public partial class TaskSettingsPageViewModel : ViewModel
{
public AllConfig Config { get; set; }
@@ -168,14 +168,6 @@ public partial class TaskSettingsPageViewModel : ObservableObject, INavigationAw
_autoFightViewModel.OnStrategyDropDownOpened(type);
}
public void OnNavigatedTo()
{
}
public void OnNavigatedFrom()
{
}
[RelayCommand]
public void OnGoToHotKeyPage()
{

View File

@@ -14,7 +14,7 @@ using Wpf.Ui.Controls;
namespace BetterGenshinImpact.ViewModel.Pages;
public partial class TriggerSettingsPageViewModel : ObservableObject, INavigationAware, IViewModel
public partial class TriggerSettingsPageViewModel : ViewModel
{
[ObservableProperty]
private string[] _clickChatOptionNames = ["优先选择第一个选项", "随机选择选项", "优先选择最后一个选项", "不选择选项"];
@@ -51,14 +51,6 @@ public partial class TriggerSettingsPageViewModel : ObservableObject, INavigatio
}
}
public void OnNavigatedTo()
{
}
public void OnNavigatedFrom()
{
}
[RelayCommand]
private void OnEditBlacklist()
{

View File

@@ -0,0 +1,36 @@
using System.Threading.Tasks;
using CommunityToolkit.Mvvm.ComponentModel;
using Wpf.Ui.Abstractions.Controls;
namespace BetterGenshinImpact.ViewModel;
public abstract partial class ViewModel : ObservableObject, INavigationAware, IViewModel
{
/// <inheritdoc />
public virtual Task OnNavigatedToAsync()
{
OnNavigatedTo();
return Task.CompletedTask;
}
/// <summary>
/// Handles the event that is fired after the component is navigated to.
/// </summary>
// ReSharper disable once MemberCanBeProtected.Global
public virtual void OnNavigatedTo() { }
/// <inheritdoc />
public virtual Task OnNavigatedFromAsync()
{
OnNavigatedFrom();
return Task.CompletedTask;
}
/// <summary>
/// Handles the event that is fired before the component is navigated from.
/// </summary>
// ReSharper disable once MemberCanBeProtected.Global
public virtual void OnNavigatedFrom() { }
}