diff --git a/src/Maui/Prism.Maui/PrismAppBuilderExtensions.cs b/src/Maui/Prism.Maui/PrismAppBuilderExtensions.cs index db60eadaa3..10133f9f8e 100644 --- a/src/Maui/Prism.Maui/PrismAppBuilderExtensions.cs +++ b/src/Maui/Prism.Maui/PrismAppBuilderExtensions.cs @@ -1,7 +1,7 @@ -using Prism.Ioc; +using Microsoft.Extensions.Logging; using Prism.Modularity; +using Prism.Mvvm; using Prism.Navigation; -using Microsoft.Extensions.Logging; namespace Prism; @@ -72,4 +72,73 @@ public static PrismAppBuilder ConfigureLogging(this PrismAppBuilder builder, Act configureLogging(builder.MauiBuilder.Logging); return builder; } + + public static PrismAppBuilder ConfigureViewTypeToViewModelTypeResolver(this PrismAppBuilder builder, Func viewModelTypeResolver) + { + ViewModelLocationProvider.SetDefaultViewTypeToViewModelTypeResolver(viewModelTypeResolver); + return builder; + } + + /// + /// Registers an with a callback that will be invoked on the UI thread for the specified . + /// + /// The . + /// An + /// The callback to invoke when the is triggered. + /// The . + public static PrismAppBuilder RegisterAppAction(this PrismAppBuilder builder, AppAction appAction, Func callback) + { + builder.MauiBuilder.ConfigureEssentials(essentials => + { + essentials.AddAppAction(appAction) + .OnAppAction(async action => + { + if (appAction.Id != action.Id) + return; + + var app = Application.Current; + if (app?.Handler?.MauiContext?.Services is null || app.Dispatcher is null) + return; + + var container = app.Handler.MauiContext.Services.GetRequiredService(); + var navigation = container.Resolve(); + await app.Dispatcher.DispatchAsync(() => + { + return callback(container, navigation, action); + }); + }); + }); + return builder; + } + + /// + /// Registers an with a callback that will be invoked on the UI thread for the specified . + /// + /// The . + /// An + /// The callback to invoke when the is triggered. + /// The . + public static PrismAppBuilder RegisterAppAction(this PrismAppBuilder builder, AppAction appAction, Func callback) + { + builder.MauiBuilder.ConfigureEssentials(essentials => + { + essentials.AddAppAction(appAction) + .OnAppAction(async action => + { + if (appAction.Id != action.Id) + return; + + var app = Application.Current; + if (app?.Handler?.MauiContext?.Services is null || app.Dispatcher is null) + return; + + var navigation = app.Handler.MauiContext.Services.GetRequiredService(); + await app.Dispatcher.DispatchAsync(() => + { + return callback(navigation, action); + }); + }); + }); + return builder; + } }