MAUI Hosting
MAUI apps often end up with a massive MauiProgram.cs where every plugin, service, and lifecycle hook is crammed together. Shiny MAUI Hosting lets you break that apart into modular IMauiModule classes — each responsible for its own service registration and post-build initialization. It also provides a static Host.Services accessor and platform lifecycle dispatch so your services can respond to foreground/background events, activity results, and more.
Features
Section titled “Features”- Modular
IMauiModuleinterface — combines service registration and post-build initialization in one class - Static
Host.Servicesfor accessing the service provider from anywhere ILifecycleExecutorfor centralized platform lifecycle event dispatch- Shared lifecycle hooks:
IAppForeground,IAppBackground - Apple lifecycle hooks:
IContinueActivity,IOnFinishedLaunching - Android lifecycle hooks:
IOnApplicationCreated,IOnActivityOnCreate,IOnActivityRequestPermissionsResult,IOnActivityNewIntent,IOnActivityResult
-
Install the NuGet package:
Terminal window dotnet add package Shiny.Extensions.MauiHosting -
Create a module for each concern:
using Shiny;public class MyModule : IMauiModule{public void Add(MauiAppBuilder builder){builder.Services.AddSingleton<IMyService, MyService>();}public void Use(IPlatformApplication app){// Post-build initialization (do NOT block here)}} -
Wire up modules in
MauiProgram.cs:using Shiny;var builder = MauiApp.CreateBuilder();builder.UseMauiApp<App>().AddInfrastructureModules(new MyModule());return builder.Build();
Creating Modules
Section titled “Creating Modules”Each module implements IMauiModule with two methods:
Add(MauiAppBuilder builder)— register services, configure the builder. This runs before the app is built.Use(IPlatformApplication app)— post-build initialization. The service provider is available viaHost.Services. Do NOT block here — this runs on the main thread during app startup.
public class AnalyticsModule : IMauiModule{ public void Add(MauiAppBuilder builder) { builder.Services.AddSingleton<IAnalytics, AppCenterAnalytics>(); }
public void Use(IPlatformApplication app) { var analytics = Host.Services.GetRequiredService<IAnalytics>(); analytics.TrackEvent("AppStarted"); }}Static Host Access
Section titled “Static Host Access”After the app is built and initialized, Host.Services provides access to the service provider from anywhere:
// Resolve a service from anywhere in your appvar service = Host.Services.GetRequiredService<IMyService>();Platform Lifecycle Hooks
Section titled “Platform Lifecycle Hooks”Register services that implement lifecycle interfaces to respond to platform events. The ILifecycleExecutor dispatches events to all registered handlers automatically.
Shared (All Platforms)
Section titled “Shared (All Platforms)”[Singleton]public class AppLifecycleHandler : IAppForeground, IAppBackground{ public void OnForeground() { // App came to foreground — refresh data, reconnect, etc. }
public void OnBackground() { // App went to background — save state, pause work, etc. }}Apple (iOS / macOS)
Section titled “Apple (iOS / macOS)”[Singleton]public class DeepLinkHandler : IContinueActivity{ public bool Handle(NSUserActivity activity) { // Handle universal links, handoff, etc. return true; }}| Interface | Purpose |
|---|---|
IContinueActivity | Universal links, handoff |
IOnFinishedLaunching | App finished launching |
Android
Section titled “Android”[Singleton]public class AppStartHandler : IOnApplicationCreated{ public void Handle(Application application) { // App-level initialization }}
[Singleton]public class PermissionHandler : IOnActivityRequestPermissionsResult{ public void Handle(Activity activity, int requestCode, string[] permissions, Permission[] grantResults) { // Handle permission results }}| Interface | Purpose |
|---|---|
IOnApplicationCreated | Application creation |
IOnActivityOnCreate | Activity creation |
IOnActivityRequestPermissionsResult | Permission request results |
IOnActivityNewIntent | New intent received |
IOnActivityResult | Activity result callback |
AI Coding Assistant
Section titled “AI Coding Assistant”An AI skill is available for Shiny MAUI Hosting to help generate IMauiModule classes, configure lifecycle hooks, and follow best practices directly in your IDE.
Claude Code
claude plugin add github:shinyorg/skillsGitHub Copilot — Copy the shiny-maui-hosting skill file into your repository’s custom instructions.