Prism
If you’ve used .NET MAUI or Xamarin Forms, you’ve no doubt heard of the great & wonderful Prism Library By Dan Siegel & Brian Lagunas. The Prism Library is a framework for building loosely coupled, maintainable, and testable XAML applications in WPF, Xamarin Forms, and .NET MAUI. It’s a framework that’s been around for a long time and has a large following.
Prism has a concept called ‘Modules’. Modules are a way to break up your application into smaller, more manageable pieces. Each module can contain its own views, view models, services, etc. This allows you to build applications in a modular way and is great for large applications with many teams, but what happens if I want to share data or navigate between modules. This is where Shiny Mediator can come and help. Shiny Mediator can obviously handle the data requests between modules as we’ve shown, but what can it do for navigation?
Having each page navigation route along with all the navigation arguments be discoverable is already hard. It usually requires constants or magic strings. Shiny Mediator can help with this by providing a strongly typed ‘route’ within the contracts library provided by each module team.
-
Install Shiny.Mediator.Prism
to wherever you plan to navigate away from.
-
In your Shiny Mediator configuration, add the Prism Navigation Service
services.AddShinyMediator(cfg =>{cfg.AddPrismSupport();}); -
We now need to create a contract for the navigation route. This is a simple class that will contain the route and any arguments that are required. We pass this contract to the Prism navigation service using the type name OR the custom provided parameter shown in the contract implementation below.
public record MyPrismNavRequest(string Arg) : IPrismNavigationRequest{public string PageUri => "AnotherPage";public string? NavigationParameterName => null;public INavigationService? Navigator { get; set; }}; -
Just like all other mediator calls, let’s use this thing
IMediator mediator; // injected// this will cause a navigationawait mediator.Send(new MyPrismNavRequest("Hello World"));