name: shiny-maui-hosting description: Generate and configure Shiny MAUI Hosting for .NET - modular MAUI app configuration with IMauiModule, static Host.Services access, and platform lifecycle hooks auto_invoke: true triggers:
- "IMauiModule"
- "Shiny.Extensions.MauiHosting"
- "ILifecycleExecutor"
- "IAppForeground"
- "IAppBackground"
- "IContinueActivity"
- "IOnFinishedLaunching"
- "IOnApplicationCreated"
- "IOnActivityOnCreate"
- "IOnActivityRequestPermissionsResult"
- "IOnActivityNewIntent"
- "IOnActivityResult"
- "Host.Services"
Shiny MAUI Hosting Skill
Triggers
- IMauiModule
- Shiny.Extensions.MauiHosting
- ILifecycleExecutor
- IAppForeground
- IAppBackground
- IContinueActivity
- IOnFinishedLaunching
- IOnApplicationCreated
- IOnActivityOnCreate
- IOnActivityRequestPermissionsResult
- IOnActivityNewIntent
- IOnActivityResult
- Host.Services
You are an expert in Shiny Extensions MAUI Hosting, a .NET library providing modular MAUI app configuration via IMauiModule with a static service provider accessor and platform lifecycle hooks.
When to Use This Skill
Invoke this skill when the user wants to:
- Create MAUI hosting modules with
IMauiModule - Use platform lifecycle hooks (
IAppForeground,IAppBackground, etc.) - Access the service provider via
Host.Services - Handle platform-specific events (activity results, universal links)
Library Overview
Documentation: https://shinylib.net/extensions/mauihost/
Repository: https://github.com/shinyorg/Shiny.Extensions
Package: Shiny.Extensions.MauiHosting
Namespace: Shiny
IMauiModule Interface
public interface IMauiModule
{
void Add(MauiAppBuilder builder); // Register services
void Use(IPlatformApplication app); // Post-build initialization (do NOT block)
}
Creating MAUI Modules
Each module implements IMauiModule with two methods:
Add(MauiAppBuilder builder)— register services, configure the builder. Runs before the app is built.Use(IPlatformApplication app)— post-build initialization.Host.Servicesis available here. Do NOT block — runs on the main thread.
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");
}
}
Setup
using Shiny;
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.AddInfrastructureModules(new AnalyticsModule(), new NetworkModule());
return builder.Build();
Static Host Access
After initialization, Host.Services provides access to the service provider from anywhere:
var service = Host.Services.GetRequiredService<IMyService>();
:::caution
Host.Services throws InvalidOperationException if accessed before initialization. Host.Lifecycle is internal — lifecycle dispatch is handled automatically.
:::
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)
[Singleton]
public class AppLifecycleHandler : IAppForeground, IAppBackground
{
public void OnForeground() { /* app came to foreground */ }
public void OnBackground() { /* app went to background */ }
}
| Interface | Purpose |
|---|---|
IAppForeground |
App entering foreground |
IAppBackground |
App entering background |
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
[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 |
API
public static class MauiExtensions
{
// Register MAUI modules - calls Add() on each, sets up Host initialization
public static MauiAppBuilder AddInfrastructureModules(
this MauiAppBuilder builder,
params IEnumerable<IMauiModule> modules);
}
public class Host : IMauiInitializeService
{
public static IServiceProvider Services { get; }
}
Code Generation Instructions
- One module per concern (similar to web modules)
- Keep
Add()for service registration andUse()for post-build initialization - Do NOT block in
Use()- it runs on the main thread during app startup - Use
Host.Servicesto resolve services after the app is built - Register lifecycle handlers (
IAppForeground,IAppBackground, etc.) via DI for platform events
Best Practices
- One concern per module - Separate modules for analytics, networking, auth, etc.
- Never block in Use() - If you need async work, use
Task.Runor similar - Use Host.Services sparingly - Prefer constructor injection; use
Host.Servicesonly where DI is unavailable - Register lifecycle handlers via DI - Use
[Singleton]attributes on lifecycle handler classes