MAUI
Setup
The easiest way to get going with Shiny Mediator in your .NET MAUI app is to use the UseMaui
extension method. This will install all the necessary services.
builder.Services.AddShinyMediator(cfg => cfg.UseMaui());
Event Collector
The MAUI event collector is installed by default by UseMaui
. Event collectors allow event handlers that may not be registered with dependency injection/Mediator
to still participate in IMediator.Publish
calls. The MAUI event collector will navigate the navigation stack, any page or viewmodel on that page that implements
the event handler type for the event contract can now participate in the subscription.
Middleware Support
The Mediator MAUI extension comes with a lot of preinstalled default middleware (that you can opt out of). If you choose to opt-out of this middleware being installed,
simply pass false to the first argument in UseMaui
Connectivity Broadcaster
Connectivity is used on almost every page or viewmodel in an app. You almost always have to inject the .NET MAUI IConnectivity service and hook the StateChanged event to do things like disabling buttons, showing errors, or putting up an banner to let you users know why your app isn’t showing fresh data. That’s a lot of extra code, cleanup, hooks, etc. This is where connectivity broadcaster comes in
builder.Services.AddShinyMediator(cfg => cfg.AddConnectivityBroadcaster());
// or Pagepublic class MyViewModel : Shiny.Mediator.IEventHandler<Shiny.Mediator.ConnectivityChanged>{ public async Task Handle(ConnectivityChanged @event, EventContext<ConnectivityChanged> context, CancellationToken ct) { // that's it SomeString = @event.Connected ? "Connected" : "Disconnected"; }}