A Microsoft framework for building cross-platform mobile apps using .NET and C# with native performance and user interfaces.
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.