dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
21.98k stars 1.71k forks source link

[Spec, WiP] Provide better hooks for shell so users can better tune services and service scopes #21816

Open PureWeen opened 4 months ago

PureWeen commented 4 months ago

Description

brunck commented 4 months ago

Would rolling back the PR listed above still allow people to inject their viewmodels into the page constructors?

Assuming yes, another thing to consider is that people don’t always want to inject viewmodels, so it’s best to allow a parameterless constructor approach as well, if that still aligns with the goals here.

PureWeen commented 2 months ago

@brunck I don't totally follow your questions

You can already inject viewmodels into the page ctors on shell if everything is registered You can also already have parameterless contructors

ederbond commented 2 months ago

Any news of when we can expect this spec to materialize into a real production ready implementation? .NET 9, 10, 11? I've been asking for this since early previews of .NET 6 and nothing until now.

albyrock87 commented 1 month ago

Regarding

Provide a better hook for creating a new MauiContext (service scope) for different parts of shell. This will give us the ability to "dispose" of service scopes which will enable users to trigger "IDisposable" paths.

I think the current API already provides RouteFactory and TypeRouteFactory classes which could be subclassed by ScopedRouteFactory:

class ScopedTypeRouteFactory : TypeRouteFactory
{
    public ScopedTypeRouteFactory(Type type) : base(type) { }

    public override Element GetOrCreate(IServiceProvider services)
    {
        var scope = services.CreateScope();
        var scopedServices = scope.ServiceProvider;
        var page = (Page)base.GetOrCreate(scopedServices);

        // Attach to some event which is triggered only when the page is removed the navigation stack
        page.RemovedFromNavigation += (s, e) =>
        {
            scope.Dispose();
        };

        return page;
    }
}

and then we can offer:

public static void RegisterScopedRoute(string route, Type type)
{
    RegisterRoute(route, new ScopedTypeRouteFactory(type));
}

It would be great to have an event at Page level which is triggered only when page is removed from navigation task. This would solve a lot of problems.

ederbond commented 1 month ago

Any news around this?

ederbond commented 1 month ago

It would be great to have an event at Page level which is triggered only when page is removed from navigation task. This would solve a lot of problems.

That would be amazing @albyrock87

ederbond commented 1 month ago

It has been +2 years since I've reported this bug for MSFT and they didn't come up with a proper solution, so to save your precious time consider completely ditching MAUI AppShell for now, and use the following library to workaround this issue: https://github.com/naveasy/Naveasy

albyrock87 commented 1 month ago

@ederbond I've seen a few PRs improving the current Shell API coming in .NET9, but I don't have a complete overview at the moment.

Regarding Shell, I don't think it's necessary "bad": it covers a lot of uses cases.

I've created a library on top of it, you may take a look here: https://github.com/nalu-development/nalu

Cheers

ederbond commented 1 month ago

Hi @albyrock87 I've seen your library like a month ago, it's cool. I'm just not a big fan of being forced to do unnecessary inheritance on my Views/ViewModels so as a former contributor of the Prism library I've decided to create my own library to workaround it. At the current state, I see MAUI Shell as a very bad implementation, it's not acceptable to rely on something that doesn't allow you dispose your objects in a simple out-of-the-box way. This is a showstopper for me.

Lucasian20 commented 1 month ago

@PureWeen Any update on this?