简体   繁体   English

Xamarin Forms CarouselView 循环绑定不起作用

[英]Xamarin Forms CarouselView Loop Binding Doesn't work

I have a collection bound to my CarouselView and I want to make the Loop property of the CarouselView to be true only when there is more than 1 element in the collection.我有一个绑定到我的CarouselView的集合,我想让CarouselViewLoop属性仅在集合中有超过 1 个元素时才为真。 This should be very easy but I don't know why it isn't working这应该很容易,但我不知道为什么它不起作用

<CarouselView ItemsSource="{Binding FoodCards}" Loop="{Binding IsLooping}">

and in my ViewModel I have a command that's executed when I press a certain button to show only one element and set IsLooping to False在我的ViewModel我有一个命令,当我按下某个按钮以仅显示一个元素并将IsLooping设置为False时执行

IsLooping = FoodCards.Count > 1;

Can this effect be achieved?能达到这个效果吗? Or we cannot change Loop during runtime?或者我们不能在运行时更改Loop

EDIT:编辑:

The workaround from the answers below works if I don't change the collection dimensions.如果我不更改集合尺寸,则以下答案中的解决方法有效。 Right now I have a collection of 3 elements bound to the CarouselView and I also have a button with a command bound to it.现在我有一个绑定到CarouselView的 3 个元素的集合,我还有一个绑定了命令的按钮。 The code from the command is:该命令的代码是:

void OnChangeLoop(){
            Items.Clear();
            Items.Add("item1");
            Items.Add("item2");
            Loop = !Loop;
        }

and in the code behind I have the workaround with the PropertyChanged .在后面的代码中,我有PropertyChanged的解决方法。 And when I press the button I get java.Lang.IllegalArgumentException: 'Invalid target position当我按下按钮时,我得到java.Lang.IllegalArgumentException: 'Invalid target position

This is a known bug, you can follow the progress on https://github.com/xamarin/Xamarin.Forms/issues/13706这是一个已知的bug,你可以关注https上的进度://github.com/xamarin/Xamarin.Forms/issues/13706

Workaround (adapted from the linked issue)解决方法(改编自链接的问题)

Code-behind代码隐藏

YourPage()       //Constructor
{
      InitializeComponent();
      BindingContext = new YourViewModel();
      (BindingContext as YourViewModel).PropertyChanged += Vm_PropertyChanged;
}

void Vm_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
    if (e.PropertyName == nameof(YourViewModel.IsLooping))
    {
        carousel.Loop = ((YourViewModel)BindingContext).IsLooping;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM