Home > Net >  Login page for .NET MAUI
Login page for .NET MAUI

Time:11-02

I have a simple .NET MAUI application and am trying to implement a login page. The goal is nothing more than to show the login page and when the user submits name and password it will go to a view model that will then pass the user along to MainPage. This works, but when it gets to MainPage there are no tabs. My Appshell.xaml looks like this:

<?xml version="1.0" encoding="UTF-8" ?>

<ShellItem>
    <ShellContent ContentTemplate="{DataTemplate view:Login}" />
</ShellItem>


<TabBar >
    <ShellContent
        Title="Home"
        ContentTemplate="{DataTemplate local:MainPage}"
        Icon="icon_home" />

    <ShellContent
        Title="About"
        ContentTemplate="{DataTemplate local:About}"
        Icon="icon_about" />

   </TabBar>

Is there an obvious solution to this issue?

Thanks!

CodePudding user response:

Here is one way to solve this:

  • Remove "Login" from Shell xaml.
  • In App.xaml.cs, there is a line MainPage = new AppShell();. Replace this with MainPage = new Login();.
  • When login succeeds, do Application.Current.MainPage = new AppShell(); - the line that was originally in App.xaml.cs.

CodePudding user response:

Inside the TabBar, I use Tab and inside it - ShellContent.

<TabBar>
        <Tab Title="..."
             Route="HomePage"
             Icon="...">
            <ShellContent
                Title="..."
                ContentTemplate="{DataTemplate view:HomePage}" />
        </Tab>
</TabBar>

Navigation after login happens with:

await Shell.Current.GoToAsync($"//{nameof(HomePage)}");

No change of Application main page is required. Navigation between your Shell components works perfectly fine. Avoid using the word "new" as much as you can. The two slashes clear the stack, so the user cannot navigate back to the login page, without using sign out button.

(Think for a second, if you need to pass data from login page, eula page, or something else to the main page, how you will do it. And then tell me if it is smart to construct your AppShell...)

  • Related