Share via

Get current ContentPage name

Anonymous
2022-04-25T12:57:18.133+00:00

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

Developer technologies | .NET | Xamarin

1 answer

Sort by: Most helpful
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 36,456 Reputation points Microsoft External Staff
    2022-04-26T05:21:34.247+00:00

    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://learn.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.

    Was this answer helpful?

    0 comments No comments

Your answer

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