0

I have a .Net MAUI app with CommunityToolkit.Mvvm that uses shell nagivation:

<FlyoutItem> 
    ...
    <ShellContent Title="Video Chat"
                  Icon="{StaticResource IconVideoChat}"
                  ContentTemplate="{DataTemplate videoChat:VideoChatPage}"
                  FlyoutItemIsVisible="False" 
                  IsVisible="{Binding IsVideoChatVisible}" />
    ...
</FlyoutItem>

Upon clicking on this item, VideoChatPage opens. I need before opening that page to check if the user is logged in, and if not, let them do it. If the user fails to do it (cancels from the login screen), the page should not open. How can I do that? How can I intercept the shell item click?

Ken White
  • 123,280
  • 14
  • 225
  • 444
David Shochet
  • 5,035
  • 11
  • 57
  • 105

1 Answers1

1

You could override AppShell OnNavigating method to achieve this.

protected override void OnNavigating(ShellNavigatingEventArgs args)
{
    base.OnNavigating(args);
    if (Any Condition)
    {
        //Then won't navigate
        args.Cancel();
    }
}

To get the next page, you may use args.Target.Location.OriginalString.

First you could set a Route for a FlyoutItem (this time set it to 'Home'):

<FlyoutItem Title="Home">
   <Tab>
       <ShellContent ContentTemplate="{DataTemplate local:MainPage}" Route="Home" />
   </Tab>
</FlyoutItem>

Then you may check the args.Target.Location.OriginalString.

protected override void OnNavigating(ShellNavigatingEventArgs args)
{
    base.OnNavigating(args);
    // if navigate to "Home/MainPage", then it is true
    if (args.Target.Location.OriginalString.Contains("Home"))
    {
        ....
    }
}

Hope it works.

Liqun Shen-MSFT
  • 3,490
  • 2
  • 3
  • 11