Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
StepKie opened this issue Jun 17, 2022 · 13 comments
Labels
area-controls-collectionview CollectionView, CarouselView, IndicatorView platform/android 🤖 s/needs-attention Issue has more information and needs another look t/bug Something isn't working

Comments

@StepKie
Copy link

StepKie commented Jun 17, 2022

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 #8135 (comment))
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

@StepKie StepKie added s/needs-verification Indicates that this issue needs initial verification before further triage will happen t/bug Something isn't working labels Jun 17, 2022
@StepKie
Copy link
Author

StepKie commented Jun 17, 2022

Also relates maybe to #7747

@jfversluis
Copy link
Member

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

@jfversluis jfversluis added platform/android 🤖 s/needs-info Issue needs more info from the author area-controls-collectionview CollectionView, CarouselView, IndicatorView and removed s/needs-verification Indicates that this issue needs initial verification before further triage will happen labels Jun 17, 2022
@ghost
Copy link

ghost commented Jun 17, 2022

Hi @Hottemax. We have added the "s/needs-info" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

@innomarcoItalia
Copy link

if it helps, issue that has occurred since version 6.0.400.
This problem is also visible in CollectionView in the project:
https://github.com/dotnet-presentations/dotnet-maui-workshop

@ghost ghost added s/needs-attention Issue has more information and needs another look and removed s/needs-info Issue needs more info from the author labels Jun 17, 2022
@StepKie
Copy link
Author

StepKie commented 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.

@StepKie
Copy link
Author

StepKie commented Jun 17, 2022

Update for platforms:

  • Also did not work on physical Android device
  • It works on iOS (simulator)
  • It works when launching as Windows app

So it really seems to be Android-specific

@StepKie

This comment was marked as off-topic.

@jfversluis

This comment was marked as off-topic.

@StepKie
Copy link
Author

StepKie commented Jun 17, 2022

Another update:

I don't want to open another ticket for this, so I will add it here, maybe the collectionview tag needs to be removed as the issue seems more general:

The same behavior of UI not updating can be observed when updating other visual elements from a Binding.
I amended the zip File with an example where a Label/ActivityIndicator bind IsVisible to a ViewModel property

TestApp.zip

Observed Behavior:
They correctly appear on iOS and Windows when the Binding updates, but not on Android

Expected Behavior:
They should appear for all platforms

     <Label
        HorizontalOptions="Center"
        IsVisible="{Binding IsBusy}"
        Text="I am only showing for 3 seconds when ShowBusyCommand runs" />
      <ActivityIndicator
        HeightRequest="40"
        HorizontalOptions="FillAndExpand"
        IsEnabled="{Binding IsBusy}"
        IsRunning="{Binding IsBusy}"
        IsVisible="{Binding IsBusy}" />
      <Button
        Command="{Binding ShowBusyCommand}"
        HorizontalOptions="Center"
        Text="Show Activity" />
        public bool IsBusy { get; set; } = false;

        public ICommand ShowBusyCommand => new AsyncCommand(ShowBusyIndicator);

        async Task ShowBusyIndicator()
        {
            // Label and ActivityIndicator will only appear on iOS and Windows, not Android
            IsBusy = true;
            OnPropertyChanged(nameof(IsBusy));
            await Task.Delay(3000);
            IsBusy = false;
            OnPropertyChanged(nameof(IsBusy));
        }

@StepKie

This comment was marked as off-topic.

@StepKie StepKie changed the title CollectionView does not update items from a Binding [Android] CollectionView and other control's visibility do not update from Binding Jun 17, 2022
@janseris
Copy link

janseris commented Jun 17, 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?

@Symbai
Copy link
Contributor

Symbai commented Jun 17, 2022

bind IsVisible to a ViewModel property

That's already trackd in #8044 for which a PR already exist #8136

@hartez
Copy link
Contributor

hartez commented Jun 25, 2022

As @Symbai mentioned, this bug is fixed by PR #8136, which is slated for Service Release 2 (coming soon).

@hartez hartez closed this as completed Jun 25, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Jul 25, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-controls-collectionview CollectionView, CarouselView, IndicatorView platform/android 🤖 s/needs-attention Issue has more information and needs another look t/bug Something isn't working
Projects
None yet
Development

No branches or pull requests

6 participants