Check Page By Name

jrahma 111 Reputation points
2021-02-09T18:27:23.623+00:00

Hi,

In Xamarin Forms, How can I check the current page by name, something like this:

if (App.Current.MainPage == MyAddress)
{
          / do something      
}

Thanks,
Jassim

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,366 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,091 questions
0 comments No comments
{count} votes

Accepted answer
  1. Joe Manke 1,091 Reputation points
    2021-02-09T18:35:25.013+00:00

    Odds are your MainPage will not be the actual page you want, but a MasterDetailPage or NavigationPage or TabbedPage or Shell, so you need to dig a little deeper. Besides that, using the type of the page is probably the best option.

    // Assuming your MainPage is a NavigationPage and MyAddress is the class name of the page you want
    var navPage = Application.Current.MainPage as NavigationPage;
    if (navPage?.CurrentPage is MyAddress)
    {
        // do something
    }
    
    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

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.