question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Android] CollectionView and other control's visibility do not update from Binding

See original GitHub issue

Description

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:closed
  • Created a year ago
  • Reactions:1
  • Comments:13 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
Hottemaxcommented, Jun 17, 2022

Does it help if you wrap it in a call to run this code on the main thread?

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.

2reactions
janseriscommented, Jun 18, 2022

Don’t get me wrong, I want you to go over to .NET MAUI and I think that’s the right thing to do, but what is stopping you from fixing those bugs and provide PRs? Forms is still maintained, those bugs you mention might not be our (top) priority, but if you say it’s important to you I am more than willing to give it a review and add it to the product. You’d be helping yourself and others. And of course that exact same goes for .NET MAUI.

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?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why do Switch and ListView controls in MAUI not update ...
The problem I'm facing is that changes to the view model are not visually propagated to the Switch.IsToggled and ListView.SelectedItem ...
Read more >
CollectionView has visibility problems - Microsoft Q&A
In my app I have several pages/views that are shown. In one the views there is a CollectionView. When the app loads this...
Read more >
Xamarin.Forms CollectionView Data
CollectionView can be populated with data by using data binding to bind its ItemsSource property to an IEnumerable collection.
Read more >
CollectionView Selection-Indicator not visible
Hello, I am using two CollectionViews in my MAUI Project and i recently noticed, that the Selection-Indicator is not visible anymore.
Read more >
Make your life simpler with the CollectionView's Layouts
CollectionView allows us display a list of data. It's very similar to the ListView but this control enhances the performance and memory management...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found