0

I am having this problem where my navigation just seems to stop working in my Maui app. I generally use:

Shell.Current.GoToAsync(route);

to go between pages.

But every now and then it just stops working. Most of the time there are no error messages or any exceptions being thrown, but sometimes I do get strange exceptions.

Also, I have one page in particular which I get stuck on every time I navigate to it. I just can't seem to navigate anywhere from it. I tried using ".." as the route but that doesn't work either.

I just have the feeling I am missing something.

My app is probably a bit too complex to upload a working example, but the shell looks like below. The page I have trouble with is the SelectorPage. I navigate to it using:

Shell.Current.GoToAsync("SelectorPage");

then on that page I try to navigate back using these:

Shell.Current.GoToAsync("LoginPage");
Shell.Current.GoToAsync("..");

but nothing happens. Putting a breakpoint on the code shows me the code is executed.

Any thoughts?

<?xml version="1.0" encoding="UTF-8" ?>
<Shell x:Class="xxx.AppShell"
            xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
            xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
              FlyoutHeaderBehavior="CollapseOnScroll"
            FlyoutWidth="300"             >
  
<!-- Initial load goes to LoadingPage, which then determines route to take -->
    <ShellItem FlyoutItemIsVisible="False" x:Name="LoadingPageShellItem">
        <ShellContent ContentTemplate="{DataTemplate views:LoadingPage}" Route="LoadingPage" />
    </ShellItem>

    <FlyoutItem Title="Home" FlyoutIcon="homemenu.png" x:Name="MainPageShellItem">

        <ShellContent Title="Home" Icon="home.png" ContentTemplate="{DataTemplate views:HomeMainPage}" Route="HomeMainPage"/> 
        <ShellContent Title="Notifications" Icon="alert.png" ContentTemplate="{DataTemplate views:NotificationsPage}" Route="NotificationsPage" />

        <!--Split tab for message page-->
        <Tab Title="Chat" Icon="msg.png">
            <ShellContent Title="Messages" ContentTemplate="{DataTemplate views:MessagesPage}" Route="MessagesPage" />
            <ShellContent Title="Message Board" ContentTemplate="{DataTemplate views:MessageBoardPage}" Route="MessageBoardPage" />
        </Tab>

    </FlyoutItem>

    <FlyoutItem Title="Login" FlyoutIcon="logoff.png" x:Name="LoginPageShellItem" FlyoutItemIsVisible="False">
        <ShellContent ContentTemplate="{DataTemplate views:LoginPage}" Route="LoginPage" />
    </FlyoutItem>

</Shell>


EDIT: I have a working example here if anyone cares to have a look: https://pdscode.com.au/wp-content/uploads/2023/02/MauiApp3.zip

I can't reproduce the exact problem in this but I do see glitches. The app has 16 'wizard' pages. After that you go to a login page which takes you to the main app. From the burger menu you can choose Reset to go back to the start. I notice that stops working after you go through a couple of times. However, hopefully someone will be able to look and tell me if I have done something terribly wrong!

Thanks

BuckBuchanan
  • 198
  • 13
  • Can you tell me on which platform your Maui program encounters this problem? Your SelectorPage is a normal ContentPage right. – Zack Feb 27 '23 at 05:56
  • It is a normal ContentPage. I am seeing it on an Android emulator. Haven't tried other platforms yet. – BuckBuchanan Feb 27 '23 at 08:42
  • I wrote a Maui project and used the same navigation as yours and didn't reproduce your problem. I tested it on windows, iOS and Android. Can you provide some codes about `SelectorPage` and `LoginPage`? – Zack Feb 28 '23 at 05:11
  • Hi @DongzhiWang-MSFT - I have uploaded and example, and explained more in the main question. Thanks – BuckBuchanan Feb 28 '23 at 11:39

3 Answers3

1

I had a similar issue where routing just stopped working. I was debugging my code line by line and all of a sudden I reached "the end of the call stack" and it went back to the UI. What was really happening though was a NULL reference exception was being thrown and Visual Studio did not alert me or break on the exception being thrown. It was being swallowed by Visual Studio. Took many additional try/catch blocks to find it. I had a object I was using when the new content page was loading that was null and I didn't properly handle that case.

If you have a similar problem where routing just stops this could be something to look out for as it seems Visual Studio will not always tell you about exceptions.

JSlice
  • 11
  • 1
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 27 '23 at 09:50
0

Try it this way:

Shell.xaml

<ShellContent ContentTemplate="{DataTemplate views:LoginPage}" Route="login_page" /> 

Shell.xaml.cs

public partial class AppShell : Shell
{
    public AppShell()
    {
        InitializeComponent();
        Routing.RegisterRoute("selector_page", typeof(SelectorPage));
    }
}

For example, you want to go from page LoginPage to page SelectorPage: in the codebehind(LoginPage) or in the LoginViewModel write the full path

Shell.Current.GoToAsync("//login_page/selector_page");

You can also enable the navigation bar for page SelectorPage so you can then navigate back to page LoginPage

public partial class SelectorPage : ContentPage{
protected override void OnAppearing() {
        base.OnAppearing();
        Shell.SetNavBarIsVisible(this, true);
    }

    protected override void OnDisappearing() {
        base.OnDisappearing();
        Shell.SetNavBarIsVisible(this, false);
    }
  }

Maybe this option will help you: Switch App Shell Tab programmatically to current page on tab, not with GotoAsync - Xamarin Forms/Maui App Shell

UPDATE: MainShellItem refers to the last NewPage16 enter image description here enter image description here

I added a little to your Shell for more convenient debugging. Added new menu item with handler https://github.com/Octopon/MauiApp3 enter image description here When debugging, pay attention to the method NavigateShell(string shellitemName)

enter image description here

  • Thanks for the reply. To be honest I don't fully understand how the routing works. It isn't really explained well in the docs. I do have all my pages routed like this: Routing.RegisterRoute(nameof(SelectorPage), typeof(SelectorPage));. But, with or without that routing nothing seems to be any different. In my case everything works most of the time except this one page. Also, the information on the link I don't think is a solution for me because it's using code behind rather than VM. – BuckBuchanan Feb 27 '23 at 00:27
  • I would try to create a TestPage without additional logic and call it instead of the SelectorPage with same route. If the problem remains, then we understand that the problem is related to Shell. Does the problem page have a long nesting route? I would also try to experiment by changing all dependencies to Transient if you are using dependency injection and see what happens. I'm new to Maui so I would experiment to see what the problem is. – dreamboatDev Feb 27 '23 at 10:04
  • Please keep me updated on this issue, as this issue is of great interest to me as well. Given that I don't have your project, I can't reproduce the problem myself, but I may run into it later in my application. – dreamboatDev Feb 27 '23 at 10:04
  • Hi @dreamboatDev. I have uploaded and example, and explained more in the main question. Thanks – BuckBuchanan Feb 28 '23 at 11:40
  • Hi @BuckBuchanan . I updated answer and added a link to the project – dreamboatDev Mar 01 '23 at 16:06
  • Hi @dreamboatDev. Sorry - I can't quite work out what i'm supposed to be looking at... I downloaded your updated version. I can see the original problem isn't there when I use the new menu item, but now I get stuck on 2 pages and can't navigate away. Can you maybe clarify a bit what the problem is? Thanks – BuckBuchanan Mar 01 '23 at 21:30
  • Hi @BuckBuchanan. It seems to me that the place where you can see the problem is the NavigateShell method. When a value("MainShellItem") is passed to this method instead of the main page route, the internal FindByName method gets the last page(NewPage16). The problem is not in this method, you can just see it here. Best practice solutions, I still don’t know how you can redirect to the main page instead of the last 16. Perhaps you need to properly clear the stack of pages before going to the main one, but these are just thoughts no more – dreamboatDev Mar 02 '23 at 12:34
  • @BuckBuchanan The question is off topic. How did you implement login? With the help redirect to IdentityServer? – dreamboatDev Mar 02 '23 at 12:37
  • Hi @dreamboatDev. Ok I see what you are saying. So it is the FindByName which is failing. I decided to raise a bug with the Maui team because at very least there needs to be some kind of exception or information about this. As for login - at the moment we just made our own login functionality. – BuckBuchanan Mar 04 '23 at 04:34
0

I checked and tested your code. When you navigate a page, you can reset and return normally, but it will fail when you have multiple pages. In the Shell documentation:

When a route from the Shell visual hierarchy is navigated to, a navigation stack isn't created. However, when a page that's not in the Shell visual hierarchy is navigated to, a navigation stack is created.

There is no navigation stack in the shell, so you can't go back to the first page. You can try to use TabbedPage to realize the navigation of multiple pages.

Zack
  • 1,255
  • 1
  • 2
  • 5
  • Hmmm sorry, I think I am really missing something here. I really don't understand the responses to this question. I think my example is fairly similar to the one in the microsoft docs. Are you saying this layout won't work and I need to do it a different way? I just don't really understand why it works, and then for no reason it doesn't work. Even if I had an error at least that would be something, but I think I must be missing something because I just don't even understand what everyone is telling me... – BuckBuchanan Mar 02 '23 at 08:23
  • Your program goes to the login page after 16 "wizard" pages, you try to go directly to the login page on the first page everything works fine, because there is no navigation stack in the shell, you go through multiple "wizards" and cannot return to First page. – Zack Mar 02 '23 at 08:41
  • It's not as simple as that. Everything works as expected. I can go back to the start after the 16 wizard pages. It is just that after a while it just stops navigating. The question I have is why. I don't think anyone has supplied anything close to an answer yet, unless I have misunderstood... – BuckBuchanan Mar 02 '23 at 10:21
  • In your code, Reset in Flyout will return to the first page for the first time, and then Reset will be invalid after 16 wizards, right? – Zack Mar 03 '23 at 09:03
  • Hi @Dongzhi Wang-MSFT - correct. But also there are other issues like the back button not working on some pages, but do work on others. – BuckBuchanan Mar 04 '23 at 04:35
  • I tested your code and got the same problem, you can report this issue on GitHub. – Zack Mar 06 '23 at 08:12