Search code examples
c#xamlmaui

How to make CarouselView respond to CurrentItemChanged in MAUI?


So I'm trying to work with CarouselView and it's been really complicated. After some researches I see that there are a lot of bugs but I haven't been able to work around this simple scenario:

I want to know about the currently selected value, and know when it changes. So I did the following:

 <CarouselView VerticalOptions="Center" PeekAreaInsets="65" Loop="True"
               ItemsSource="{Binding Minutes}" CurrentItem="{Binding SelectedMinute}" CurrentItemChanged="carouselMinutes_CurrentItemChanged">
 <CarouselView.ItemsLayout>

Out of the box, CurrentItem property does not work at all that it why I registered to the CurrentItemChanged event, but when I scroll it is never raised. Any input is appreciated, thanks a lot.

EDIT: updating to .NET 8 preview solved my issues and I am now able to properly work with the CarouselView control.


Solution

  • Here is a small demo to fire the CurrentItemChanged event, you may have a try.

    <Label x:Name="mylabel" TextColor="Red" FontSize="50"/>
    
    <CarouselView VerticalOptions="Center" PeekAreaInsets="65" Loop="True"
          ItemsSource="{Binding Items}"
                  CurrentItem="{Binding CurrentItem}"
                  CurrentItemChanged="CarouselView_CurrentItemChanged">
    

    And each time we scroll the CarouselView, we change the text of label:

    void CarouselView_CurrentItemChanged(System.Object sender, Microsoft.Maui.Controls.CurrentItemChangedEventArgs e)
    {
    
        mylabel.Text = viewModel.CurrentItem.Name;
    }
    

    Here is the effect:

    enter image description here