[Android] CollectionView and other control's visibility do not update from Binding
See original GitHub issueDescription
The Content of a CollectionView
bound to a property via DataTemplate
will only update when the user interacts with the CollectionView
control via a drag gesture. It does not refresh the UI itself via OnPropertyChanged(nameof(BoundProperty))
.
Code snippets (reproducible from attached zip) Test project:
- was created from the default MAUI template
- adding a simple MainViewModel, binding in codebehind of MainPage
- ObservableObject from CommunityToolkit.Mvvm was used as the only dependency for INotifyPropertyChanged (the issue is the same for the Xamarin.CommunityToolkit.MauiCompat version)
<CollectionView ItemsSource="{Binding Items}"
<CollectionView.ItemTemplate>
<DataTemplate>
<Label Text="{Binding .}" />
</StackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
internal class MainPageViewModel : ObservableObject
{
private int counter = 0;
public ObservableCollection<string> Items { get; set; } = new() { "0" };
public ICommand UpdateItemsCommand => new Command(UpdateItems);
void UpdateItems()
{
Items.Add((++counter).ToString());
OnPropertyChanged(nameof(Items)); // Not strictly necessary as it is ObservableCollection
}
}
Steps to Reproduce
Open the provided TestApp.zip (edited to include the issue described in https://github.com/dotnet/maui/issues/8135#issuecomment-1158781384) TestApp.zip
Click the “Update Icons” button. It will show that the Items list has been updated. The UI of the CollectionView will only refresh to show the added items when performing a Drag Gesture (i.e. Click in the CollectionView area below the Label, and drag).
Version with bug
6.0.400 (current)
Last version that worked well
Unknown/Other
Affected platforms
Android, I was not able test on other platforms
Affected platform versions
Android API 30, 31
Did you find any workaround?
No response
Relevant log output
No response
Issue Analytics
- State:
- Created a year ago
- Reactions:1
- Comments:13 (4 by maintainers)
Top GitHub Comments
Hi Gerald, I have tried all kinds of Thread magic to get it to work (using AsyncCommand, Task.Delay, ConfigureAwait(false), Device.BeginInvokeOnMainThread(), and a great number of random combinations of these, but nothing yielded any results. The UI gets notified (as can be observed in the attached sample project by seeing that the LastAdded binding is correctly updated in the UI from the same method, but the CollectionView just refuses to cooperate.
I feel that this should really be high priority, as CollectionView not updating is kind of a deal-breaker.
Does seriously a Microsoft employee ask a customer to fix a bug in a Microsoft framework (sounding like it’s the customer’s responsibility to get it fixed)? MAUI is not someone’s hobby project which we should be helping developing for fun all together. It is a serious official product like ASP.NET Core, EF Core etc. Should Windows users also fix bugs which they find in Windows 11 maybe? Can you see how absurd it sounds?