Xamarin.Forms Shell application: By default, it bypass Login Page

Jerry Lipan 916 Reputation points
2022-05-30T03:11:21.757+00:00

Hi. I'm Xamarin Newbies. Now using AppShell to start development. So far, I've this

206556-30052022-002.png

206500-30052022-001.png

 public partial class App : Application  
    {  
  
        public App()  
        {  
            InitializeComponent();  
  
            DependencyService.Register<MockDataStore>();  
            MainPage = new AppShell();  
        }  
  

1st page running, it goes to AppShell with AboutPage.xaml

206532-30052022-003.png

In AppShell.xaml, I can see this,

  <FlyoutItem Title="About" Icon="icon_about.png">  
        <ShellContent Route="AboutPage" ContentTemplate="{DataTemplate local:AboutPage}" />  
    </FlyoutItem>  
    <FlyoutItem Title="Browse" Icon="icon_feed.png">  
        <ShellContent Route="ItemsPage" ContentTemplate="{DataTemplate local:ItemsPage}" />  
    </FlyoutItem>  
  
    <!-- When the Flyout is visible this will be a menu item you can tie a click behavior to  -->  
    <MenuItem Text="Logout" StyleClass="MenuItemLayoutStyle" Clicked="OnMenuItemClicked">  
    </MenuItem>  

If I change the order of FlyoutItem as following,

  <FlyoutItem Title="Browse" Icon="icon_feed.png">  
        <ShellContent Route="ItemsPage" ContentTemplate="{DataTemplate local:ItemsPage}" />  
    </FlyoutItem>  
    <FlyoutItem Title="About" Icon="icon_about.png">  
        <ShellContent Route="AboutPage" ContentTemplate="{DataTemplate local:AboutPage}" />  
    </FlyoutItem>  

I got this,

206543-30052022-004.png

At this level, I managed to understand AppShell

Now, I want 1st page running is LoginPage.xaml

206478-30052022-005.png

Then, user can click Login to store appropriate value before go to AppShell with Default Page

How to modify below code ?

App.xaml.cs

 public App()  
        {  
            InitializeComponent();  
  
            DependencyService.Register<MockDataStore>();  
            MainPage = new AppShell();              
        }  
  1. If user don't have appropriate value, it redirect to LoginPage.xaml
  2. If user having an appropriate value, it go to AppShell with Default Page

Please help me to achieve my objective

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,296 questions
0 comments No comments
{count} votes

Accepted answer
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 26,451 Reputation points Microsoft Vendor
    2022-05-31T05:14:51.083+00:00

    Hello @Jerry Lipan ,

    I'm not very clear about what the appropriate value means, you could use Xamarin.Essentials: Preferences to store the value and check if user is logged when the app starts. If the user is logged, go to new AppShell(), else go to new LoginPage(). I store a bool value, refer to the following code:

    Boolean isLogin = Preferences.Get("isLogin", false);  
                if (isLogin)  
                {  
                    MainPage = new AppShell();  
                   Shell.Current.GoToAsync("//ItemsPage"); //go to AppShell with Default Page(ItemsPage), you also could go to other pages  
                }  
                else  
                {  
                    MainPage = new LoginPage();  
                }  
    

    When click login:

    private void async OnLoginClicked(object obj)  
            {  
                Preferences.Set("isLogin", true);  
                Application.Current.MainPage = new AppShell();  
                Shell.Current.GoToAsync("//ItemsPage");   
            }  
    

    In addition, there is a closed thread discussing how to do Login, Onboarding, and similar scenarios, you could refer to the solution.

    Best Regards,
    Wenyan Zhang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful