У меня проблема с Xamarin.Forms с ListView (и CollectionView, который я тоже пробовал). У меня были некоторые проблемы с производительностью (ветка на форуме XF), которые были решены, но у меня есть еще одна проблема: элементы не обновляются в «реальном времени» при изменении их свойств.
У меня есть ObservableCollection of Category ( который является коллекцией Score) и другой ObservableCollection с оценками, для свойства IsFavorite которых установлено значение true. Класс Score реализует INotifyPropertyChanged (и, очевидно, установка IsFavorite вызывает событие). Я отображаю их в 2 ListViews, на 2 разных вкладках (используя TabbedPage): каждая ячейка имеет некоторый текст и кнопку с текстом, привязанным к Score.IsFavorite.
Проблема: когда я пу sh это Кнопка, список 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; }
}
ViewModel:
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);
}
}
И View:
<?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, который фактически использует одиночный экземпляр для совместного использования между двумя страницами ...)
У кого-нибудь есть идея?
Спасибо, Оливье