ListView绑定仅在滚动时更新UI

Aph*_*ire 1 xaml listview observablecollection inotifypropertychanged xamarin.forms

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

单击时,我正在尝试更改按钮的颜色,可以看到事件正在触发,并且PropertyChanged可以按预期工作,但是在我向下滚动ListView之前,按钮不会更改颜色,因此不再在视图中,然后向上滚动到该视图。当我向后滚动时,我认为这会触发某种“让我知道您的状态,以便我可以向您显示”事件,然后该事件会进行更改。

以下是我的XAML:

<ListView CachingStrategy="RecycleElement" 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>
Run Code Online (Sandbox Code Playgroud)

您可能会注意到,我正在使用CachingStrategy="RecycleElement" 它可能与这个问题有关,这背后的原因已在此处的上一篇SO帖子中进行了解释。

本质上,使用默认的CachingStrategy会导致意外结果。

Ger*_*uis 5

要立即更新视图,您将必须INotifyPropertyChanged在模型上实现接口。使用此接口,只要属性值发生更改,它将通知UI并进行相应更新。

每当您滚动时,它都会更新的原因是该单元已重新粉刷,甚至完全重新填充。

不要误以为使用ObservableCollection遗嘱就能做到,因为那只会反映集合结构中的更新,而不反映集合结构中的模型。