Hello,
You could set a Title
on your page and use it to determine if the current page is MainPage
:
// in xaml
<ContentPage ...
x:Class="MauiApp14.NewPage1"
Title="NewPage1">
// in c#
var name = AppShell.Current.CurrentPage.Title;
if (name.Equals("MainPage"))
{
Console.WriteLine("MainPage");
}else
{
Console.WriteLine(name);
}
Update:
This problem occurs because you put var name = AppShell.Current.CurrentPage.ClassId;
is placed in the page's constructor, and the CurrentPage
has not yet been assigned, so this error is caused.
For any way to get page properties, a more recommended way is to put them in the page's OnAppearing
method.
protected override void OnAppearing()
{
base.OnAppearing();
var name = AppShell.Current.CurrentPage.ClassId;
if ...
}
Best Regards,
Alec Liu.
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.