How to navigate independent from Flyout definiton in Xamarin.Forms Shell at app start

begbeg Member ✭✭

In our company's Xamarin.Forms application, we are moving from Prism Navigation to Shell Navigation.

The first thing to do is Navigating to a blank StartPage without content, then there is a logic that decides if the user should go through the OnBoarding process or not, and after that the user is Navigated to the HomePage...
The StartPage shouldn't be part of the Flyout. the user is not allowed to navigate there manually.

How can i realise this logic in Xamarin.Forms Shell?

i already tried with await Current.GoToAsync("StartPage"); in both OnNavigated and OnNavigating methods, but Current is null at this point

Best Answer

  • begbeg ✭✭
    edited August 8 Accepted Answer

    i solved this with setting the StartPage as MainPage at startup:
    App.xaml.cs
    MainPage = new StartPage();

    then, in the StartPage, i have implemented the logic what should be next. if the OnBoarding process needs to be done, i Navigate modally to the OnBoarding without Shell:

    StartPage.xaml.cs
    await Navigation.PushModalAsync(new OnBoarding());

    if there is no OnBoarding need to be done, i Navigate to the ShellPage:

    StartPage.xaml.cs
    Application.Current.MainPage = new AppShell();

Answers

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    If you want to navigate anyone page at the app start, you can register this navigation page in the OnStart method in the App.xaml.cs, Then navigate it.

     protected override void OnStart()
        {
            // Handle when your app starts
    
            Routing.RegisterRoute("monkeydetails", typeof(MonkeyDetailPage));
            Shell.Current.GoToAsync("monkeydetails");
    
        }
    
  • NMackayNMackay GBInsider, University mod

    @beg said:
    In our company's Xamarin.Forms application, we are moving from Prism Navigation to Shell Navigation.

    The first thing to do is Navigating to a blank StartPage without content, then there is a logic that decides if the user should go through the OnBoarding process or not, and after that the user is Navigated to the HomePage...
    The StartPage shouldn't be part of the Flyout. the user is not allowed to navigate there manually.

    How can i realise this logic in Xamarin.Forms Shell?

    i already tried with await Current.GoToAsync("StartPage"); in both OnNavigated and OnNavigating methods, but Current is null at this point

    Yikes, that's a brave choice.

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    @beg Are there any updates for this issue? If the reply is helpful, please try to mark it as an answer, it will help others who have similar issue.

  • begbeg Member ✭✭
    edited August 8 Accepted Answer

    i solved this with setting the StartPage as MainPage at startup:
    App.xaml.cs
    MainPage = new StartPage();

    then, in the StartPage, i have implemented the logic what should be next. if the OnBoarding process needs to be done, i Navigate modally to the OnBoarding without Shell:

    StartPage.xaml.cs
    await Navigation.PushModalAsync(new OnBoarding());

    if there is no OnBoarding need to be done, i Navigate to the ShellPage:

    StartPage.xaml.cs
    Application.Current.MainPage = new AppShell();

  • LeijaeLeijae Member ✭✭

    But how would one solve the logout dilemma?

    Yea, okay I don't want you to see any navigational abilities until you log in.... no problem, use your method; easy peasy.

    but what if a user logs out... life cycle, I don't believe allows you to kill the shell or start it over... right?

  • begbeg Member ✭✭

    @Leijae
    i think you can then set the MainPage to the LoginPage, just like me setting it to StartPage

    Application.Current.MainPage = new LoginPage();

    or, when you define the logout button as Menuitem in the Flyout, and bind it to a command, you can push the login page modally over the shellpage, so the user is forced to login. this logic is basically the same like mine pushing the onboarding page.

    <MenuItem Text="logout" Command="{Binding LogoutCommand}" />

    LogoutCommand action
    await Navigation.PushModalAsync(new LoginPage());

  • AlbertKAlbertK MYMember ✭✭✭

    Have you tested it extensively if your method does not leak memory?. I am using your method for onboarding but mine is done once per app lifetime so I have not tested what happens if you do multiple cycle of login -> logout loop.

  • begbeg Member ✭✭

    @AlbertK i am also doing this once. the answer i posted regarding login/logout was just a suggestion for @Leijae on how to solve it.
    in our app, the user is not necessary to be logged in to browse so we are using a separate page for that. and the login process is to select a zip code and choose delivery or pickup. we are implementing an eStore app!

  • LeijaeLeijae Member ✭✭

    @beg fair point. I suppose though, we just need to wait on the xamarin team to easily allow dynamic menu items if a menu should be different for logged in vs anonymous users

Sign In or Register to comment.