Share via

Xamarin Forms CarouselView trying to Bind Loop property

Gordon S 501 Reputation points
2021-04-27T15:04:18.203+00:00

I have a CarouselView with Loop enabled, which works fine for 2 or more items. However, it doesn't work well when there is only 1. It's weird that you can scroll and keep getting the same item!

Anyway, I have added code to check the number of items and set Loop to True if items > 1, otherwise it is set to false.

However, even though the code runs to set the value of Loop, the CarouselView itself does not change.

I am using a ViewModel which has a "Base" model containing a "SetProperty" method where "OnPropertyChanged" is called.

Should I be able to dynamically change the Loop setting, or is it set once only?

Developer technologies | .NET | Xamarin
0 comments No comments

Answer accepted by question author

JessieZhang-MSFT 7,721 Reputation points Microsoft External Staff
2021-04-28T07:28:46.423+00:00

Hello,

Welcome to our Microsoft Q&A platform!

Update 2

Yes, it is just the case as you said. If we set an initial value for property Loop in CarouselView, once we change the value for the binded value, the UI couldn't update accordingly.

 <CarouselView ItemsSource="{Binding items}" Loop="{Binding EnableCarouselLoop}" >  

And this is a known issue , you can follow it up here:https://github.com/xamarin/Xamarin.Forms/issues/13706 .

Update 1

Should I be able to dynamically change the Loop setting, or is it set once only?

I have a CarouselView with Loop enabled, which works fine for 2 or more items. However, it doesn't work well when there is only 1. It's weird that you can scroll and keep getting the same item!

Anyway, I have added code to check the number of items and set Loop to True if items > 1, otherwise it is set to false.

However, even though the code runs to set the value of Loop, the CarouselView itself does not change.

I couldn't fully understand what you mean. Does the Loop in the item of the CarouselView ?And under what circumstances do you want to change this value?

Of course, you can change the Loop setting after you init a value for it first.
Take the Xamarin.Forms - CarouselView for example, we can add the variable (Loop)in the item model, just as the Monkey.cs do(e.g. IsFavorite) :

public class Monkey  
{  
    public string Name { get; set; }  
    public string Location { get; set; }  
    public string Details { get; set; }  
    public string ImageUrl { get; set; }  
    public bool IsFavorite { get; set; }  
    // add variable Loop here  
    public bool Loop { get; set; }   
}  

And in page HorizontalSwipeItemsPage.xaml ,we can trigger command FavoriteCommand in viewmode MonkeysViewModel.cs

         <SwipeView.TopItems>  
                           <SwipeItems>  
                                  <SwipeItem Text="Favorite"  
                                           IconImageSource="favorite.png"  
                                           BackgroundColor="LightGreen"  
                                           Command="{Binding Source={x:Reference carouselView}, Path=BindingContext.FavoriteCommand}"  
                                          CommandParameter="{Binding}" />  
                         </SwipeItems>  
        </SwipeView.TopItems>  

The FavoriteCommand is:

   public ICommand FavoriteCommand => new Command<Monkey>(FavoriteMonkey);  
    void FavoriteMonkey(Monkey monkey)  
    {  
        monkey.IsFavorite = !monkey.IsFavorite;  
    }  

Here, we can change the value of IsFavorite . So, you can do the same thing for the variable Loop .

Best Regards,

Jessie Zhang


If the response is helpful, please click "Accept Answer" and upvote it.

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 additional answers

Sort by: Most helpful

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.