Hello,
The CarouselPage
has been superseded by the CarouselView, you could change the Title
and by CurrentItemChanged event when the current item changes. Refer to this sample and the following code:
XAML
<ContentPage.BindingContext>
<ViewModels:CarouselViewViewModel/>
</ContentPage.BindingContext>
<NavigationPage.TitleView >
<StackLayout >
<Label x:Name="TitleLabel" Text="Home" VerticalOptions="Center" />
</StackLayout>
</NavigationPage.TitleView>
<CarouselView x:Name="carouselView" ItemsSource="{Binding Monkeys}" PositionChanged="carouselView_PositionChanged" CurrentItemChanged="carouselView_CurrentItemChanged"
>
<CarouselView.ItemTemplate>
<DataTemplate>
......
</DataTemplate>
</CarouselView.ItemTemplate>
</CarouselView>
CodeBehind
Monkey currentItem;
public void UpdateUI() {
TitleLabel.Text = $"Current item: {currentItem?.Name}";// title
((NavigationPage)Application.Current.MainPage).BarBackgroundColor = currentItem.PageColor;// color in the viewmodel
}
private void carouselView_CurrentItemChanged(object sender, CurrentItemChangedEventArgs e)
{
currentItem = e.CurrentItem as Monkey;
UpdateUI();
}
ViewModel
public class CarouselViewViewModel
{
readonly IList<Monkey> source;
public ObservableCollection<Monkey> Monkeys { get; private set; }
public CarouselViewViewModel()
{
source = new List<Monkey>();
CreateMonkeyCollection();
}
void CreateMonkeyCollection()
{
source.Add(new Monkey
{
PageColor = Color.Red
});
source.Add(new Monkey
{......
});
.......
Monkeys = new ObservableCollection<Monkey>(source);
}
}
public class Monkey
{
......
public Color PageColor { get; set; }
}
You also could try to use a command to respond to the current item changing
<CarouselView ItemsSource="{Binding Monkeys}"
CurrentItemChangedCommand="{Binding ItemChangedCommand}"
CurrentItemChangedCommandParameter="{Binding Source={RelativeSource Self}, Path=CurrentItem}">
...
</CarouselView>
-------- CarouselPage Update(do the same thing by CurrentPageChanged
event)--------
Push to CarouselPage
private void Button_Clicked(object sender, EventArgs e)
{
Navigation.PushAsync(new MyCarouselPage());
}
CurrentPageChanged
event
private void CarouselPage_CurrentPageChanged(object sender, EventArgs e)
{
var index = this.Children.IndexOf(this.CurrentPage);
ColorsDataModel model = ColorsDataModel.All[index];
TitleLabel.Text = model.Name;
((NavigationPage)Application.Current.MainPage).BarBackgroundColor = model.Color;
}
XAML
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.