LiteRegion.WPF 1.2.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package LiteRegion.WPF --version 1.2.0
                    
NuGet\Install-Package LiteRegion.WPF -Version 1.2.0
                    
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="LiteRegion.WPF" Version="1.2.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="LiteRegion.WPF" Version="1.2.0" />
                    
Directory.Packages.props
<PackageReference Include="LiteRegion.WPF" />
                    
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 LiteRegion.WPF --version 1.2.0
                    
#r "nuget: LiteRegion.WPF, 1.2.0"
                    
#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 LiteRegion.WPF@1.2.0
                    
#: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=LiteRegion.WPF&version=1.2.0
                    
Install as a Cake Addin
#tool nuget:?package=LiteRegion.WPF&version=1.2.0
                    
Install as a Cake Tool

LiteRegion

LiteRegion 是一个为 WPF 打造的轻量级、灵感来源于 Prism 的区域导航和对话框框架,基于 Microsoft.Extensions.DependencyInjection 构建。

特性

  • 区域导航 (Region-based Navigation):通过定义区域(Regions)并在此之间导航 View 和 ViewModel,实现 UI 的解耦。
  • 依赖注入 (Dependency Injection):原生支持 Microsoft.Extensions.DependencyInjection,无缝集成。
  • 对话框服务 (Dialog Service):轻松显示模态对话框,并支持向 ViewModel 传递参数和获取返回值(回调)。
  • 导航生命周期 (Navigation Lifecycle):实现 INavigationAwareIConfirmNavigation 接口,以实现对导航过程的精细控制(如拦截导航、传参)。

基础使用

1. 配置服务和 LiteRegion

App.xaml.cs 中,设置依赖注入容器,使用 AddLiteRegion 注册你的 View 和 ViewModel。然后调用 UseLiteRegion() 来初始化框架。

public partial class App : Application
{
    public IServiceProvider Services { get; }

    public App()
    {
        Services = ConfigureServices().UseLiteRegion();
    }

    private static IServiceProvider ConfigureServices()
    {
        var services = new ServiceCollection();
        
        // 注册其他服务或 ViewModel
        services.AddTransient<MainWindowViewModel>();
        
        // 配置 LiteRegion
        services.AddLiteRegion(builder =>
        {
            // 注册用于导航的视图(默认以单例 Singleton 模式注册 View 和 ViewModel)
            builder.RegisterForNavigation<ViewA, ViewAViewModel>();
            builder.RegisterForNavigation<ViewB, ViewBViewModel>();
            
            // 注册需要在后台持续运行的导航视图(开启 preload 预加载,在初始化时自动实例化)
            builder.RegisterForNavigation<LogView, LogViewModel>(preload: true);
            
            // 注册对话框(默认以瞬态 Transient 模式注册 View 和 ViewModel)
            builder.RegisterDialog<SampleDialogView, SampleDialogViewModel>();
        });

        return services.BuildServiceProvider();
    }
}

2. 在 XAML 中定义区域

使用 lr:RegionManager.RegionName 附加属性在布局中定义一个区域。确保添加了 xmlns:lr="http://schemas.literegion.com/wpf" 命名空间。

<Window x:Class="LiteRegion.MainWindowView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:lr="http://schemas.literegion.com/wpf">
    <Grid>
        
        <ContentControl lr:RegionManager.RegionName="ContentRegion" />
    </Grid>
</Window>

3. 在视图间导航

在你的 ViewModel 中注入 INavigationService 并使用它进行导航。配合 MVVM Toolkit 可以在 XAML 中绑定按钮的 Command。

ViewModel (C#):

using CommunityToolkit.Mvvm.Input;

public partial class MenuViewModel
{
    private readonly INavigationService _navigationService;

    public MenuViewModel(INavigationService navigationService)
    {
        _navigationService = navigationService;
    }

    [RelayCommand]
    public void NavigateToViewA()
    {
        // 在 "ContentRegion" 区域内导航到 "ViewA"
        _navigationService.NavigateTo("ContentRegion", "ViewA");
        
        // 或者使用强类型方法:
        // _navigationService.NavigateTo<ViewAViewModel>("ContentRegion");
    }
}

View (XAML): 在 XAML 中,通过按钮的 Command 属性绑定到对应的命令:

<UserControl x:Class="LiteRegion.Views.MenuView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <StackPanel>
        <Button Content="打开视图 A" Command="{Binding NavigateToViewACommand}" />
    </StackPanel>
</UserControl>

4. 显示对话框

在 ViewModel 中注入 IDialogService 来显示模态对话框。与对话框关联的 ViewModel 可以实现 IDialogAware 来处理参数和关闭请求。

ViewModel (C#):

using CommunityToolkit.Mvvm.Input;

public partial class MyViewModel
{
    private readonly IDialogService _dialogService;

    public MyViewModel(IDialogService dialogService)
    {
        _dialogService = dialogService;
    }

    [RelayCommand]
    public void OpenDialog()
    {
        _dialogService.ShowDialog("SampleDialogView", parameters: "Hello", callback: result => 
        {
            // 处理对话框返回结果
        });
    }
}

View (XAML): 同样地,在 XAML 中绑定该命令:

<Button Content="弹出对话框" Command="{Binding OpenDialogCommand}" />

许可证

本项目基于 MIT 许可证开源 - 详情请查看 LICENSE 文件。

Product Compatible and additional computed target framework versions.
.NET net6.0-windows7.0 is compatible.  net7.0-windows was computed.  net8.0-windows was computed.  net8.0-windows7.0 is compatible.  net9.0-windows was computed.  net10.0-windows was computed. 
.NET Framework net472 is compatible.  net48 was computed.  net481 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
2.2.0 125 6/25/2026
1.2.0 123 4/1/2026
1.1.0 114 3/29/2026
1.0.1 150 3/28/2026