Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Enhancement] Allow UsePrism to be called multiple times #3274

Open
dansiegel opened this issue Nov 4, 2024 · 1 comment
Open

[Enhancement] Allow UsePrism to be called multiple times #3274

dansiegel opened this issue Nov 4, 2024 · 1 comment

Comments

@dansiegel
Copy link
Member

Summary

As requested by @aritchie, there may be times in which you want to write extensions for the MauiAppBuilder to bring in features and those features would benefit from modifying the PrismAppBuilder as the example below shows adding a module.

public static MauiAppBuilder AddMapsFeature(this MauiAppBuilder builder)
{
    builder.UsePrism(x => x.ConfigureModuleCatalog(modules => modules.AddModule<MapsModule>()));
    return builder;
}

This is currently not recommended in Prism 9.0 as each call to UsePrism initializes a new instance of the PrismAppBuilder and can prevent you from successfully getting callbacks such as Navigating when CreateWindow is called or callbacks for OnInitialized.

Proposed API

I believe we want to ensure that the behavior of UsePrism remains intact as this is also important for Unit Testing. To provide the proper behavior we can instead look at introducing something like:

public static class PrismAppBuilderExtensions
{
    // Ensure that `UsePrism` is called first
    public static MauiAppBuilder ConfigurePrism(this MauiAppBuilder builder, Action<PrismAppBuilder> configure)
    {
        var prism = ContainerLocator.Container.Resolve<PrismAppBuilder>();
        configure(prism);
        return builder;
    }
}
@dansiegel dansiegel added this to the 9.1 milestone Nov 4, 2024
@aritchie
Copy link

aritchie commented Nov 4, 2024

The extension needed a bit of adjustment to check if the container was initialized. You can have other calls calling UsePrism elsewhere for this to function perfectly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants