本文介绍了绑定到ObservableCollection的Xamarin ListView随机更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下问题:

我有一个使用ViewModels和XAML绑定的Xamarin Forms项目.我有一个绑定到特定对象的ObservableCollection的特定ListView.

I have a Xamarin Forms project, using ViewModels and XAML binding. I have a particular ListView that is bound to an ObservableCollection of a particular object.

对象包含显示以下按钮列表所需的所有信息:

The objects contain all the required information to display the following list of buttons:

<ListView ItemsSource="{Binding ListViewItems}" Margin="0,20,0,0" RowHeight="130" SeparatorVisibility="None" VerticalOptions="FillAndExpand" Grid.Column="1" Grid.Row ="0" >
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <Button Command="{Binding HeatClickedCommand}" Margin="5,10,5,10" HorizontalOptions ="FillAndExpand" BackgroundColor="{Binding color_hex}" Grid.Column="1" TextColor="{StaticResource LightTextColor}" FontSize="Medium" Text="{Binding heat_title}"></Button>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

第一次加载页面时,根据特定的应用程序状态和有关对象的信息,colour_hex设置为绿色,灰色或红色.首次显示该列表时,屏幕上适合的所有按钮似乎都能正确显示,但是末尾的按钮似乎不显示(应该将最后4个按钮显示为灰色,并且在Debug中,我已经回显了颜色,应该是灰色).

When the page first loads, the colour_hex is set to either green, grey or red depending on a particular app state and information about the object. When the list is first displayed, all of the buttons that fit on the screen appear to display correctly, however the buttons at the end appear not to (The last 4 are supposed to be greyed out, and in Debug i've echo'd the colour and it should be grey).

但是,奇怪的是,如果某些按钮是绿色的,则快速上下滚动似乎可以更改,其中哪些按钮被随机设置为绿色.

The strange thing however, is that if some of the buttons are green, scrolling up and down quickly appears to change which buttons are set to green at random.

例如,说按钮1和2和红色,但是说3是绿色,如果我向下滚动到说15,然后快速回到顶部,它可能已经改变,所以现在2是绿色,而3是红色.

For example, say buttons 1 and 2 and red, but 3 is green, if I scroll down to say 15, and then back up to the top quickly, it may have changed so that 2 is now green and 3 red.

这似乎只发生在android系统(手机和平板电脑)上,但是我还没有在足够多的设备上进行过测试,还不能确切地说出这一点.我只知道Windows上不会发生这种情况.

This only appears to happen on androids (phone and tablet), but I haven't tested it across a wide enough variety of devices yet to be able to conclusively say that. I just know it doesn't happen on Windows.

检查调试"后,似乎没有任何颜色更改功能或属性更改功能被触发,这看起来像是某种内存问题.

Checking Debug it looks that none of the colour changing functions or propertychanged functions are firing when this is happening, it looks to be like some kind of memory problem.

任何对此的建议,将不胜感激.

Any advise on this would be greatly appreciated.

推荐答案

看看ListView.CachingStrategy属性及其作用.这可能是造成这种情况的原因.

Have a look at the ListView.CachingStrategy property and what it does. That is probably what is causing this.

此设置确定如何重用单元格.如果将其设置为错误的值(针对您的用例),则可能会导致某些奇怪的行为.

This setting determines how cells are being reused. If set to a wrong value (for your use-case) it might cause some strange behaviour.

更多信息可在文档中找到: https://developer .xamarin.com/api/type/Xamarin.Forms.ListViewCachingStrategy/

More information can be found in the documentation: https://developer.xamarin.com/api/type/Xamarin.Forms.ListViewCachingStrategy/

这篇关于绑定到ObservableCollection的Xamarin ListView随机更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 22:17