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

Questions/issue #1

Open
LeoJHarris opened this issue Nov 25, 2024 · 9 comments
Open

Questions/issue #1

LeoJHarris opened this issue Nov 25, 2024 · 9 comments

Comments

@LeoJHarris
Copy link

LeoJHarris commented Nov 25, 2024

Hi there,

First off great plugin, we have just replaced Prism Navigation with MVVM Zero, it was exactly what we were looking for as a replacement, just a few questions/issue to raise:

  • Better support for MVVM community toolkit? We actually forked the project and made MvvmZeroBaseVm extend ObservableObject, thus decorating properties with the ObservableProperty attribute. With this change we could better extend MvvmZeroBaseVm, would you consider adding a dependancy to CommunityToolkit.Mvvm?
  • Potentially an issue but when calling (MultiPage<Page>?)pageServiceZero.GetMultiPage a second time such as resetting the MainPage to a TabbedPage again, the Children of the TabbedPage get messed up. Should calling this a second time simply reset the MainPage?
  • FYI MainPage is set to be deprecated from .NET 9 https://learn.microsoft.com/en-us/dotnet/maui/whats-new/dotnet-9?view=net-maui-9.0#mainpage
@Keflon
Copy link
Owner

Keflon commented Nov 25, 2024

Hi Leo
First, thanks for the message; it's nice to hear when people other than myself are using the libraries! You may be interested that I'll be releasing a .NET 9 update along with some cool improvements once I've updated the documentation.

I'd rather not add a dependency to the community toolkit, but I can see how it would be useful, so let me think about it for a bit.

As for your bug report, I'll take a look at the weekend - I've never replaced the MainPage at runtime myself so I'll try it out and fix what breaks.

It would be helpful if you can tell me whether your MultiPage, ContentPages and ViewModels are singleton or transient when registered in the container.

Also, are you using a TabbedPage, or an AdaptedTabbedPage or something else? If you're not using AdaptedTabbedPage could you give it a go and let me know if it solves your problem?

builder.Services
    // The root page is supplied by the container!
    // AdaptedTabbedPage Because https://github.com/dotnet/maui/issues/14572
    .AddSingleton<MultiPage<Page>, AdaptedTabbedPage>()

    .AddSingleton<ReadyPage>()

    ...

@LeoJHarris
Copy link
Author

@Keflon not a problem! There has been a discussion regarding page lifecycles made available via MVVM here dotnet/maui#120 but the only way I've seen this provided was via Prism or as a DIY. But we also wanted some sort of MVVM first navigation system and we don't use shell for the simple reason it does not support TabbedPage otherwise we might have opted for shell navigation.

Also, are you using a TabbedPage, or an AdaptedTabbedPage or something else? If you're not using AdaptedTabbedPage could you give it a go and let me know if it solves your problem?

I was not using the AdaptedTabbedPage earlier but have tried subclassing and the same issue persists.

First time MainPage was set:

Untitled

Resetting MainPage:

Untitled

You can see there ends up becoming 10 children, I'm not sure if the original TabbedPage got its children removed?

@LeoJHarris
Copy link
Author

@Keflon forgot to update you they are registered as Singleton:

ServiceCollectionServiceExtensions.AddSingleton<MultiPage<Page>, LandingTabbedPage>(serviceCollection);
serviceCollection.AddSingleton<CurrentPasswordPage, CurrentPasswordPageViewModel>();
serviceCollection.AddSingleton<UpdateCustomerNamePage, UpdateCustomerNamePageViewModel>();

@Keflon
Copy link
Owner

Keflon commented Dec 2, 2024

Hi @LeoJHarris Thanks for the info. Unfortunately my day job took my weekend from me so I'll be looking at this next weekend.

@LeoJHarris
Copy link
Author

@Keflon Did you have any time to check this?

@Keflon
Copy link
Owner

Keflon commented Dec 27, 2024

@LeoJHarris Hi, happy holidays!
I found some time to take a look and used a timer to swap MainPage between a TabbedPage and a plain ContentPage every few seconds.
For me, setting the TabbedPage a second time caused a crash in the GetMultiPage call, and it's clear that the TabbedPage had its previous children and the crash occurred whilst it was attaching to a second set of identical children.
This looks like it fits with your results, though I'm not sure if yours then crashed? I expect it does?
I'm reluctant to say this is the TabbedPage misbehaving, but at the moment that looks to be the case. The library provides an ItemsSource to the TabbedPage as well as an ItemTemplate, and the ItemsSource has the correct number of ViewModels, so the TabbedPage appears to be erroneously retaining the previous children.
You may be able to dodge this by making your TabbedPage transient, so that GetMultiPage returns a new instance each time you call it, but that may also require you make your child pages transient too, depending on whether the 'old' TabbedPage correctly releases them when it is removed from the visual tree.
I'll see if I can find a workaround that can be applied to the library, but at the moment this looks like it's a MAUI problem, and I remember from my Xamarin days that it was often problematic replacing the MainPage after initially setting it, so you could perhaps try a root NavigationPage and push/pop the MultiPage? Note this would be a bad idea if you try to push a FlyoutPage - they don't like it if they're not the root page in the hierarchy.

@LeoJHarris
Copy link
Author

@Keflon I did not encounter any crashing only the first child of the tabbed page showed a blank screen, tab selection stopped working and duplicate tab items were appearing. I'm not sure what the best solution is at this stage, I'm not familiar with any issues in MAUI affecting resetting the MainPage due to the fact we use Prism to manage navigation which easily resets it when https://prismlibrary.github.io/docs/xamarin-forms/navigation/navigation-basics.html#navigating

I've not tried this myself but wondering if its possible to reset the MainPage like Application.Current.MainPage = new MainPage() and then call GetMultiPage again as if it was called for the first time?

@Keflon
Copy link
Owner

Keflon commented Jan 6, 2025

@LeoJHarris Thanks for the feedback, I'll take a look. I may not find time before the coming weekend; if it's a blocker for you let me know and I'll see if I can get to it sooner, but I can't make any promises.

@LeoJHarris
Copy link
Author

LeoJHarris commented Jan 6, 2025

@LeoJHarris Thanks for the feedback, I'll take a look. I may not find time before the coming weekend; if it's a blocker for you let me know and I'll see if I can get to it sooner, but I can't make any promises.

It's not a blocker. Actually, we are planning to drop prism, but we will continue using prism until we can find an alternative, I'd setup an experimental branch to try out your library and the only issue seems to be with resetting the nav.

Unfortunately, Xamarin and now Maui don't seem to offer out of the box MVVM first navigation functionality. The closest thing, although not actually part of the MAUI framework itself, is the MAUI community pop-ups and how that handles pushing view models.

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

No branches or pull requests

2 participants