How to get current page?

Yuan Jeffers 1 Reputation point
2022-08-26T01:28:33.013+00:00

Hi,

Application.Current.MainPage or Shell.Current always get the shell page.
How can I get the current page after user lick the shell menu?
Thanks.

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,412 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 74,491 Reputation points Microsoft Vendor
    2022-08-29T08:52:10.267+00:00

    Hello,

    Firstly, please add Route property for your <ShellContent> in the <FlyoutItem> like following code. The Route name is the same as page’s name

       <FlyoutItem Title="Cats"  
                       >  
               <Tab>  
                   <ShellContent Route="catepage" ContentTemplate="{DataTemplate local:CatsPage}" />  
               </Tab>  
           </FlyoutItem>  
    

    Then open the AppShell.xaml.cs, please override OnNavigated method. After clicking menu items, you can get the Route of current page by var currentPageRouteValues = args.Current.Location;

       protected override async void OnNavigated(ShellNavigatedEventArgs args)  
                       {  
                                       base.OnNavigated(args);  
                                       var currentPageRouteValues = args.Current.Location;  
                      await     DisplayAlert("info", currentPageRouteValues.ToString(),"OK");  
         
           }  
    

    Best Regards,

    Leon Lu


    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 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.