question

JassimAlRahma-9056 avatar image
0 Votes"
JassimAlRahma-9056 asked WenyanZhang-MSFT commented

Get current ContentPage name

Hi,

I have a CarouselPage and I added multiple ContentPages to it.

Inside the CarouselPage_CurrentPageChanged, how can I get the current ContentPage name?


Thanks,
Jassim


dotnet-xamarin
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Removed unnecessary / incorrect tags. Per your other posts on this, you're using Xamarin and not MAUI.

1 Vote 1 ·

Hi @JassimAlRahma-9056, I have not heard from you for a couple of days. Please let me know if there is anything that I can help here.

0 Votes 0 ·

1 Answer

WenyanZhang-MSFT avatar image
0 Votes"
WenyanZhang-MSFT answered

Hello,

You could set the names as an array, and get the index in CurrentPageChanged event, then you could get the name by this index. Refer to the following code :
XAML

 <?xml version="1.0" encoding="UTF-8"?>
 <CarouselPage xmlns="http://xamarin.com/schemas/2014/forms"
               xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
               x:Class="CarouselPageDemo.MyCarouselPage"
               CurrentPageChanged="CarouselPage_CurrentPageChanged">
     <NavigationPage.TitleView>
         <StackLayout>
             <Label x:Name="TitleLabel" Text="Home Page" VerticalOptions="Center" />
         </StackLayout>
     </NavigationPage.TitleView>
     <ContentPage x:Name="ContentPageHomePage">
         <StackLayout>
             <Label Text="ContentPageHomePage" FontSize="Medium" HorizontalOptions="Center" />
             <BoxView Color= "red" WidthRequest="200" HeightRequest="200" HorizontalOptions="Center" VerticalOptions="CenterAndExpand" />
         </StackLayout>
     </ContentPage>
     <ContentPage x:Name="ContentPagePag2">
     </ContentPage>
     <ContentPage x:Name="ContentPagePag3">
     </ContentPage>
     <ContentPage x:Name="ContentPagePag4">
     </ContentPage>
 </CarouselPage>

Code behind

  public static IList<String> NameSource { get; set; } = new List<String>() { "ContentPageHomePage","ContentPagePag2", "ContentPagePag3", "ContentPagePag4" };
         private void CarouselPage_CurrentPageChanged(object sender, EventArgs e)
         {
             var index = this.Children.IndexOf(this.CurrentPage);
             String name = NameSource[index];
            // TitleLabel.Text = name;
         }

If your ContentPage in CarouselPage is similar, try to use <CarouselPage.ItemTemplate>, then set the ItemsSource, and get the model. The ColorsDataModel is here : https://github.com/xamarin/xamarin-forms-samples/blob/main/Navigation/CarouselPageTemplate/CarouselPageNavigation/ColorsDataModel.cs
The XAML is here: https://github.com/xamarin/xamarin-forms-samples/blob/main/Navigation/CarouselPageTemplate/CarouselPageNavigation/MainPage.xaml

 public MyCarouselPage()
         {
             InitializeComponent();
             ItemsSource = ColorsDataModel.All;
         }
         private void CarouselPage_CurrentPageChanged(object sender, EventArgs e)
         {
             var index = this.Children.IndexOf(this.CurrentPage);
             ColorsDataModel model = ColorsDataModel.All[index];
            TitleLabel.Text = model.Name;
         }

For more details, refer to https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/navigation/carousel-page

Best Regards,
Wenyan Zhang


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.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.