XFEExtension.NetCore.WinUIHelper 1.2.5

dotnet add package XFEExtension.NetCore.WinUIHelper --version 1.2.5
                    
NuGet\Install-Package XFEExtension.NetCore.WinUIHelper -Version 1.2.5
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="XFEExtension.NetCore.WinUIHelper" Version="1.2.5" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="XFEExtension.NetCore.WinUIHelper" Version="1.2.5" />
                    
Directory.Packages.props
<PackageReference Include="XFEExtension.NetCore.WinUIHelper" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add XFEExtension.NetCore.WinUIHelper --version 1.2.5
                    
#r "nuget: XFEExtension.NetCore.WinUIHelper, 1.2.5"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package XFEExtension.NetCore.WinUIHelper@1.2.5
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=XFEExtension.NetCore.WinUIHelper&version=1.2.5
                    
Install as a Cake Addin
#tool nuget:?package=XFEExtension.NetCore.WinUIHelper&version=1.2.5
                    
Install as a Cake Tool

XFEExtension.NetCore.WinUIHelper

基于 .NET 8 的 WinUI 3 扩展库,提供了一系列便捷的帮助类、服务和扩展方法,旨在简化 WinUI 3 应用的开发流程。

目录

特性

  • 轻量级 IOC 容器:内置 ServiceManager,支持服务注册与全局单例获取。
  • 导航管理:封装 NavigationViewFrame,提供基于 ViewModel 的导航体验。
  • UI交互服务:提供 DialogServiceMessageService (类似 InfoBar)、LoadingService 等常用交互服务。
  • MVVM 支持:提供 ObservableObject 扩展及通用 ViewModel 基类。

快速开始

1. 初始化配置

App.xaml.cs 中进行初始化,包括注册页面和配置异常处理。

public App()
{
    this.InitializeComponent();
    
    // 设置应用主题
    AppThemeHelper.Theme = ElementTheme.Dark; 

    // 注册导航页面 (PageManager)
    // 必须在此处注册所有可通过字符串或类型导航的页面
    PageManager.RegisterPage(typeof(AppShellPage));
    PageManager.RegisterPage(typeof(MainPage));
    PageManager.RegisterPage(typeof(TestPage));
    
    // 全局异常捕获(可选)
    UnhandledException += App_UnhandledException;
}

private void App_UnhandledException(object sender, Microsoft.UI.Xaml.UnhandledExceptionEventArgs e)
{
    // 使用消息服务显示错误
    if (ServiceManager.GetService<IMessageService>() is IMessageService messageService)
    {
        messageService.ShowMessage(e.Message, "发生错误", InfoBarSeverity.Error);
        e.Handled = true;
    }
}

2. 构建 Shell 页

创建一个包含 NavigationView 的 Shell 页(例如 AppShellPage),并绑定相关服务。

AppShellPageViewModel.cs:

public class AppShellPageViewModel : ObservableObject
{
    // 使用 GetService 获取服务新实例
    public INavigationViewService NavigationViewService { get; set; } = ServiceManager.GetService<INavigationViewService>();
    public IMessageService MessageService { get; set; } = ServiceManager.GetService<IMessageService>();
    public ILoadingService LoadingService { get; set; } = ServiceManager.GetService<ILoadingService>();
}

AppShellPage.xaml.cs:

public sealed partial class AppShellPage : Page
{
    public AppShellPageViewModel ViewModel { get; set; } = new();

    public AppShellPage()
    {
        Current = this;
        this.InitializeComponent();

        // 1. 初始化导航服务 (绑定 NavigationView 和 Frame)
        ViewModel.NavigationViewService.Initialize(navigationView, navigationFrame);
        
        // 2. 初始化消息服务 (绑定用于显示消息的 StackPanel)
        ViewModel.MessageService.Initialize(messageStackPanel, DispatcherQueue);
        
        // 3. 初始化加载服务 (绑定 Loading 控件)
        ViewModel.LoadingService.Initialize(loadingGrid, globalLoadingGrid, globalLoadingTextBlock, DispatcherQueue, ViewModel.NavigationViewService.NavigationService);
        
        // 4. 初始导航
        ViewModel.NavigationViewService.NavigateTo<MainPage>();
    }
}

3. 使用服务

在子页面(如 MainPage)的 ViewModel 中,可以通过 ServiceManager.GetGlobalService<T>() 获取在 Shell 页已初始化的全局服务实例

public partial class MainPageViewModel : ObservableObject
{
    // 获取全局实例 (注意使用 GetGlobalService)
    public INavigationViewService? NavigationViewService { get; } = ServiceManager.GetGlobalService<INavigationViewService>();
    public IMessageService? MessageService { get; } = ServiceManager.GetGlobalService<IMessageService>();

    [RelayCommand]
    void DoSomething()
    {
        // 显示消息
        MessageService?.ShowMessage("操作成功!", "提示", InfoBarSeverity.Success);
        
        // 页面跳转
        NavigationViewService?.NavigateTo<TestPage>("传递的参数");
    }
}

核心功能

ServiceManager (服务管理器)

简单的依赖注入及服务定位器。

  • GetService<T>(): 获取服务实例。如果该服务类型遵循命名约定(如 IMyServiceMyService),则会自动创建实例。
  • GetGlobalService<T>(): 获取已注册的全局单例服务。通常继承自 GlobalServiceBase 的服务在实例化时(如在 Shell 页初始化时)会自动注册为全局单例。

导航服务 (INavigationViewService)

用于管理 NavigationView 的选中状态与 Frame 的页面跳转同步。

  • Initialize(...): 必须在使用前调用,绑定 UI 元素。
  • NavigateTo<TPage>(parameter): 导航到指定页面。
  • NavigationService.CanGoBack: 检查是否可后退。

消息与对话框

  • IMessageService: 在界面特定区域显示非阻塞通知。需在 Shell 页的 XAML 中放置一个 StackPanel 作为容器。
  • IDialogService: 显示内容对话框。
  • ILoadingService: 管理加载状态,支持页面级遮罩和全局遮罩。

常用工具类

  • PageManager: 静态类,用于注册页面类型,使导航系统能通过 Type 找到对应的页面。
  • AppThemeHelper: 用于管理应用的主题(Light/Dark/System)。
  • NavigationHelper: 提供了 SetParameter 等方法用于页面间参数传递。
Product Compatible and additional computed target framework versions.
.NET net8.0-windows10.0.19041 is compatible.  net9.0-windows was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.2.5 0 3/13/2026
1.2.4 207 11/26/2025
1.2.3 153 10/4/2025
1.2.2 207 6/22/2025
1.2.1 205 5/28/2025
1.2.0 154 5/2/2025
1.1.2 160 5/2/2025
1.1.1 184 4/25/2025
1.1.0 186 4/25/2025
1.0.7 233 4/8/2025
1.0.6 228 4/6/2025
1.0.5 205 4/6/2025
1.0.4 195 4/6/2025
1.0.3 213 4/6/2025
1.0.2 199 4/6/2025
1.0.1 195 4/6/2025
1.0.0 207 4/6/2025

命名空间重命名及版本号升级至1.2.5

     将 Utilities.Addition 重命名为 Utilities.Additions,并同步更新相关引用。项目版本号由 1.2.4 升级为 1.2.5。