【发布时间】:2020-06-19 15:03:01
【问题描述】:
我在使用 ListView(以及我也尝试过的 CollectionView)的 Xamarin.Forms 上遇到问题。我在性能方面遇到了一些麻烦(XF 论坛上的帖子),但我仍然有一个问题:当它们的属性发生变化时,项目不会“实时”更新。
我有一个类别的 ObservableCollection(它是一个分数的集合)和另一个带有 IsFavorite 属性设置为 true 的分数的 ObservableCollection。 Score 类实现了 INotifyPropertyChanged(显然设置 IsFavorite 会引发事件)。我将它们显示在 2 个 ListView 中,在 2 个不同的选项卡中(使用 TabbedPage):每个单元格都有一些文本,以及一个带有绑定到 Score.IsFavorite 的文本的 Button。
问题:当我按下此按钮时,FavoritesList 被正确修改,主 ListView 中的单元格也被修改...但是我必须向下和向上滚动以使相应的 ViewCell 消失然后重新出现,以看到变化。否则,Button.Text 会一直显示旧值!
一些代码:
Score 和 Category 类:
public class Score : HasPropertyChanging, IComparable<Score>
{
// Other properties...
// The HasPropertyChanging abstract class implements INotifyPropertyChanged
private bool _isFavorite;
public bool IsFavorite { get => _isFavorite; set => _isFavorite = SetFieldValueAndNotify(value); }
}
public class Category : ObservableCollection<Score>
{
public string Name { get; set; }
}
视图模型:
public class ScoreListViewModel : ViewModelBase
{
// Lists of categories and scores : Categories<Score> (not ordered) / all scores (order AZ) / favorites (order AZ)
public ObservableCollection<Category> Categories { get; set; } = new ObservableCollection<Category>();
public ObservableCollection<Score> Scores { get; set; } = new ObservableCollection<Score>();
public ObservableCollection<Score> FavoriteScores { get; set; } = new ObservableCollection<Score>();
public ScoreListViewModel()
{
this.InitializeScoreList();
}
// Toggle favorite status (raised from ICommand in XAML)
public void ToggleFavorites(Score score)
{
score.IsFavorite = !score.IsFavorite;
if (score.IsFavorite)
this.FavoriteScores.AddSorted(score);
else
this.FavoriteScores.Remove(score);
}
}
还有观点:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:viewmodels="clr-namespace:CardioCALC"
x:Class="CardioCALC.ScoreListPage"
x:Name="ThisPage"
x:DataType="viewmodels:ScoreListViewModel"
BindingContext="{x:Static viewmodels:ScoreListViewModel.Instance}">
<ListView
ItemsSource="{Binding Categories}"
IsGroupingEnabled="True"
GroupDisplayBinding="{Binding Name}"
HasUnevenRows="True"
CachingStrategy="RecycleElement">
<ListView.ItemTemplate>
<DataTemplate x:DataType="viewmodels:Score">
<ViewCell Height="70">
<Grid BackgroundColor="White" Padding="20, 5, 10, 5">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="1.5*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="50" />
</Grid.ColumnDefinitions>
<Label Text="{Binding DisplayName}" FontSize="17" Grid.Column="0" Grid.Row="0" />
<Label Text="{Binding Detail}" FontSize="13" Opacity="0.6" Grid.Column="0" Grid.Row="1" />
<Button Text="{Binding IsFavorite, Converter={StaticResource FavoritesDisplayValueConverter}}" Grid.Column="1" Grid.Row="0" Grid.RowSpan="2"
Clicked="OnFavButtonClicked"
Command="{Binding Source={x:Reference ThisPage}, Path=ToogleFavorites}" CommandParameter="{Binding .}" />
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentPage>
(注意:我简化了一点 ViewModel,它实际上使用一个单例实例在 2 个页面之间共享...)
有人有想法吗?
谢谢, 奥利维尔
【问题讨论】:
-
尝试修改缓存策略
-
一开始我使用的是 RetainElement(默认情况下),但我遇到了同样的问题,性能非常差......
标签: listview xamarin inotifypropertychanged