LiteObservableRegions 1.0.0
See the version list below for details.
dotnet add package LiteObservableRegions --version 1.0.0
NuGet\Install-Package LiteObservableRegions -Version 1.0.0
<PackageReference Include="LiteObservableRegions" Version="1.0.0" />
<PackageVersion Include="LiteObservableRegions" Version="1.0.0" />
<PackageReference Include="LiteObservableRegions" />
paket add LiteObservableRegions --version 1.0.0
#r "nuget: LiteObservableRegions, 1.0.0"
#:package LiteObservableRegions@1.0.0
#addin nuget:?package=LiteObservableRegions&version=1.0.0
#tool nuget:?package=LiteObservableRegions&version=1.0.0
LiteObservableRegions
A lightweight, Prism-like region and navigation library for WPF. Define regions in XAML, navigate by URI, and resolve views from Microsoft.Extensions.DependencyInjection or from named host children.
Features
- XAML-first regions — Attached properties
RegionNameandViewNameon anyDependencyObject(e.g.Grid,ContentControl). - URI-based navigation — Format
region://RegionName/TargetName?query. Navigate, redirect, go back/forward per region. - Two ways to resolve views
- Named views — Children with
ViewName="region://RegionName/View1"are registered by name; navigation to that target uses the element directly (no DI). - DI views — Register view types with
AddRegionViews; views are resolved from the service provider (Transient, Scoped, Singleton).
- Named views — Children with
- Navigation stack — Per-region back/forward history; redirect clears the back stack.
- INavigationAware — Optional callbacks
OnNavigatedFrom/OnNavigatedToon views or view-models. - Pluggable host adapter —
IRegionHostContentAdaptercontrols how content is shown (default supports ContentControl, Frame, Panel, ItemsControl, Decorator). - Region context —
RegionContextattached property for arbitrary data storage (no change handling). - Region change notification — Subscribe to
WeakReferenceRegionHub.ObservableRegionChangedbefore any content switch; receive detailed context (region name, from/to URI and target names, Navigate/Redirect/GoBack/GoForward) and optionally cancel by settingRegionChangedEventArgs.Cancel.
Installation
dotnet add package LiteObservableRegions
Targets: .NET Framework 4.6.2+, .NET 5–10 (Windows, WPF).
Quick start
1. Register services and set the service provider
In your app startup (e.g. App.xaml.cs), build the DI container and set the static service provider so XAML-attached registration can resolve IRegionManager:
using LiteObservableRegions;
using Microsoft.Extensions.DependencyInjection;
var services = new ServiceCollection();
services.AddObservableRegions(reg =>
{
reg.AddView<PageA>("ViewA", ServiceLifetime.Scoped);
reg.AddView<PageB>("ViewB", ServiceLifetime.Scoped);
});
// Optional: services.AddSingleton<IRegionHostContentAdapter, MyAdapter>();
IServiceProvider provider = services.BuildServiceProvider();
WeakReferenceRegionHub.ServiceProvider = provider;
2. Define a region and optional named views in XAML
<Grid xmlns:r="clr-namespace:LiteObservableRegions;assembly=LiteObservableRegions"
r:ObservableRegion.RegionName="region://MainRegion">
<ContentControl x:Name="Host" />
</Grid>
Or use inline named views (no DI needed for these targets):
<Grid r:ObservableRegion.RegionName="region://TestGridRegion">
<Grid r:ObservableRegion.ViewName="region://TestGridRegion/View1" />
<Grid r:ObservableRegion.ViewName="region://TestGridRegion/View2" />
</Grid>
The first path segment after the region name (e.g. View1, View2) is the target name used when navigating.
3. Navigate
IRegionManager manager = WeakReferenceRegionHub.RegionManager;
// Navigate (push onto back stack)
manager.Navigate(new Uri("region://MainRegion/ViewA"));
// Redirect (replace current, clear back stack)
manager.Redirect(new Uri("region://MainRegion/ViewB"));
// Back/forward
if (manager.CanGoBack("MainRegion"))
manager.GoBack("MainRegion");
if (manager.CanGoForward("MainRegion"))
manager.GoForward("MainRegion");
// Get region info
IRegion region = manager.GetRegion("MainRegion");
Uri current = region?.CurrentUri;
object host = region?.Host;
4. Region change notification and cancellation
Subscribe to WeakReferenceRegionHub.ObservableRegionChanged to be notified before a region’s content changes (Navigate, Redirect, GoBack, or GoForward). The event args provide full context and allow cancelling the change:
WeakReferenceRegionHub.ObservableRegionChanged += (sender, e) =>
{
// e.RegionName, e.FromUri, e.ToUri, e.FromTargetName, e.ToTargetName, e.Mode (Push/Replace/Back/Forward)
System.Diagnostics.Debug.WriteLine(
$"{e.Mode} in {e.RegionName}: {e.FromTargetName} -> {e.ToTargetName} ({e.ToUri})");
// Cancel this navigation (no stack or content change will occur)
if (someCondition)
e.Cancel = true;
};
- When: Fired before the region content is updated (before views receive
OnNavigatedFrom/OnNavigatedTo). - Cancel: Set
e.Cancel = trueto prevent the navigation; the back/forward stacks and current content stay unchanged. - Mode:
NavigationMode.Push(Navigate),Replace(Redirect),Back, orForward.
Region URIs
- Format:
region://RegionName/TargetName?param1=value1¶m2=value2 - Region name — Host part; must match a registered region (e.g. from
RegionName). - Target name — First path segment; identifies the view (either a named view under that host or a key in the view registry).
- Query — Optional; parsed and passed in
NavigationContext.Parametersand toINavigationAware.
You can use the shorthand region://MainRegion for the region name only (e.g. in RegionName); the library normalizes it. For navigation you must include a target: region://MainRegion/View1.
Helper: RegionUriParser.BuildUri(regionName, targetName, parameters).
Attached properties (ObservableRegion)
| Property | Type | Description |
|---|---|---|
| RegionName | string | Set on the host element. Registers the element as a region. Accepts "region://Name" or "Name". |
| ViewName | string | Set on a child of the host. Value like region://RegionName/View1; the child is registered as the view for View1 so navigation to that target uses it without DI. |
| CurrentContent | object | Set by the library; current view displayed in the region. Can be bound. |
| RegionContext | object | Storage for arbitrary data; no change handling. |
View resolution order
When navigating to a target name in a region:
- Named views — If the region has a view registered for that name (e.g. via
ViewName), that instance is used. - DI — Otherwise the view is resolved from
IRegionViewRegistry+IServiceProvider(Transient, Scoped per region, or Singleton).
Register view types with AddRegionViews(reg => reg.AddView<MyView>("TargetName", ServiceLifetime.Scoped)).
Customizing how content is displayed
Implement IRegionHostContentAdapter and register it in DI before AddObservableRegions. The default adapter supports:
ContentControl→ContentFrame→Navigate(content)Panel/ItemsControl→ add child and optionally toggle visibility (configurableIsPreferKeepAlive)Decorator→Child
Duplicate names
- Same RegionName — Registering again replaces the previous region: existing scope is disposed, and the new host becomes the only one for that name.
- Same ViewName (same region) — Registering again overwrites the previous named view for that target.
License
MIT
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0-windows7.0 is compatible. net6.0-windows was computed. net6.0-windows7.0 is compatible. net7.0-windows was computed. net7.0-windows7.0 is compatible. net8.0-windows was computed. net8.0-windows7.0 is compatible. net9.0-windows was computed. net9.0-windows7.0 is compatible. net10.0-windows was computed. net10.0-windows7.0 is compatible. |
| .NET Framework | net462 is compatible. net463 was computed. net47 was computed. net471 was computed. net472 is compatible. net48 is compatible. net481 is compatible. |
-
.NETFramework 4.6.2
- Microsoft.Extensions.DependencyInjection (>= 10.0.3)
-
.NETFramework 4.7.2
- Microsoft.Extensions.DependencyInjection (>= 10.0.3)
-
.NETFramework 4.8
- Microsoft.Extensions.DependencyInjection (>= 10.0.3)
-
.NETFramework 4.8.1
- Microsoft.Extensions.DependencyInjection (>= 10.0.3)
-
net10.0-windows7.0
- Microsoft.Extensions.DependencyInjection (>= 10.0.3)
-
net5.0-windows7.0
- Microsoft.Extensions.DependencyInjection (>= 10.0.3)
-
net6.0-windows7.0
- Microsoft.Extensions.DependencyInjection (>= 10.0.3)
-
net7.0-windows7.0
- Microsoft.Extensions.DependencyInjection (>= 10.0.3)
-
net8.0-windows7.0
- Microsoft.Extensions.DependencyInjection (>= 10.0.3)
-
net9.0-windows7.0
- Microsoft.Extensions.DependencyInjection (>= 10.0.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.