Conditional MainPage in AppShell

Jassim Al Rahma 1,516 Reputation points
2023-03-29T14:05:55.4833333+00:00

Hi,

I have the below in my AppShell

<?xml version="1.0" encoding="utf-8" ?>
<Shell
    x:Class="MyApp.Mobile.AppShell"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:local="clr-namespace:MyApp.Mobile"
    Shell.FlyoutBehavior="Disabled">
    <ShellContent
        Title="Home"
        ContentTemplate="{DataTemplate local:MainPage}"
        Route="MainPage" />
</Shell>

But how can I add a condition here to show the MainPage if the user is already signed in otherwise the MainPage should be Signin

My signed-in condition is stored in SecureStorage.Get("IsSignedin")

Thanks,

Jassim

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,747 questions
{count} votes

1 answer

Sort by: Most helpful
  1. betalyte 0 Reputation points
    2023-03-29T14:22:27.5666667+00:00

    I'd do the check on OnAppearing for each view:

    var isAuthenticated = SecureStorage.Get("IsSignedin");
    if (isAuthenticated) {
        await this.routingService.NavigateTo("///MainPage");
    } else {
         await this.routingService.NavigateTo("///Signin");
    }
    

    Check out this page for more details: https://mallibone.com/post/xamarin-forms-shell-login