xamarin / Xamarin.Forms

Xamarin.Forms is no longer supported. Migrate your apps to .NET MAUI.
https://aka.ms/xamarin-upgrade
Other
5.62k stars 1.87k forks source link

[Bug] CollectionView Items not displayed after refresh. #15097

Open Gh0stC0de opened 2 years ago

Gh0stC0de commented 2 years ago

Description

Items in CollectionView are not displayed after clearing and reinitializing ObservableCollection.

Steps to Reproduce

  1. Start Sample
  2. Click Refresh in Toolbar

Expected Behavior

Items are being refreshed (same items are displayed).

Actual Behavior

No Items are displayed.

Basic Information

Environment

Show/Hide Visual Studio info ``` Microsoft Visual Studio Enterprise 2022 Version 17.0.5 VisualStudio.17.Release/17.0.5+32112.339 Microsoft .NET Framework Version 4.8.04084 Installed Version: Enterprise Visual C++ 2022 00482-10000-00261-AA852 Microsoft Visual C++ 2022 .NET Core Debugging with WSL 1.0 .NET Core Debugging with WSL ASP.NET and Web Tools 2019 17.0.795.42246 ASP.NET and Web Tools 2019 ASP.NET Web Frameworks and Tools 2019 17.0.795.42246 For additional information, visit https://www.asp.net/ Azure App Service Tools v3.0.0 17.0.795.42246 Azure App Service Tools v3.0.0 Azure Functions and Web Jobs Tools 17.0.795.42246 Azure Functions and Web Jobs Tools C# Tools 4.0.1-1.21568.1+6ab6601178d9fba8c680b56934cd1742e0816bff C# components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used. Common Azure Tools 1.10 Provides common services for use by Azure Mobile Services and Microsoft Azure Tools. Conveyor by Keyoti 1.0 Allows you to access your web applications from other machines, even if hosted on the development web server. Extensibility Message Bus 1.2.6 (master@34d6af2) Provides common messaging-based MEF services for loosely coupled Visual Studio extension components communication and integration. Microsoft Azure Tools for Visual Studio 2.9 Support for Azure Cloud Services projects Microsoft JVM Debugger 1.0 Provides support for connecting the Visual Studio debugger to JDWP compatible Java Virtual Machines Microsoft Library Manager 2.1.134+45632ee938.RR Install client-side libraries easily to any web project Microsoft MI-Based Debugger 1.0 Provides support for connecting Visual Studio to MI compatible debuggers Microsoft Visual C++ Wizards 1.0 Microsoft Visual C++ Wizards Microsoft Visual Studio Tools for Containers 1.2 Develop, run, validate your ASP.NET Core applications in the target environment. F5 your application directly into a container with debugging, or CTRL + F5 to edit & refresh your app without having to rebuild the container. Microsoft Visual Studio VC Package 1.0 Microsoft Visual Studio VC Package Mono Debugging for Visual Studio 17.0.11 (54f19d2) Support for debugging Mono processes with Visual Studio. NuGet Package Manager 6.0.1 NuGet Package Manager in Visual Studio. For more information about NuGet, visit https://docs.nuget.org/ ProjectServicesPackage Extension 1.0 ProjectServicesPackage Visual Studio Extension Detailed Info Razor (ASP.NET Core) 17.0.0.2156603+e9cd763349a080127b6519ffbec0123949a4c385 Provides languages services for ASP.NET Core Razor. Snapshot Debugging Extension 1.0 Snapshot Debugging Visual Studio Extension Detailed Info SQL Server Data Tools 17.0.62110.20190 Microsoft SQL Server Data Tools TypeScript Tools 17.0.1001.2002 TypeScript Tools for Microsoft Visual Studio Visual Basic Tools 4.0.1-1.21568.1+6ab6601178d9fba8c680b56934cd1742e0816bff Visual Basic components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used. Visual F# Tools 17.0.0-beta.21522.2+6d626ff0752a77d339f609b4d361787dc9ca93a5 Microsoft Visual F# Tools Visual Studio Code Debug Adapter Host Package 1.0 Interop layer for hosting Visual Studio Code debug adapters in Visual Studio Visual Studio Container Tools Extensions 1.0 View, manage, and diagnose containers within Visual Studio. Visual Studio IntelliCode 2.2 AI-assisted development for Visual Studio. Visual Studio Tools for Containers 1.0 Visual Studio Tools for Containers VisualStudio.DeviceLog 1.0 Information about my package VisualStudio.Foo 1.0 Information about my package VisualStudio.Mac 1.0 Mac Extension for Visual Studio Xamarin 17.0.0.343 (d17-0@fb07a17) Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android. Xamarin Designer 17.0.0.183 (remotes/origin/d17-0@a351f0f1f) Visual Studio extension to enable Xamarin Designer tools in Visual Studio. Xamarin Templates 17.0.17 (9e779b0) Templates for building iOS, Android, and Windows apps with Xamarin and Xamarin.Forms. Xamarin.Android SDK 12.1.0.5 (d17-0/6b0e6b2) Xamarin.Android Reference Assemblies and MSBuild support. Mono: c633fe9 Java.Interop: xamarin/java.interop/d17-0@febb1367 ProGuard: Guardsquare/proguard/v7.0.1@912d149 SQLite: xamarin/sqlite/3.36.0@a575761 Xamarin.Android Tools: xamarin/xamarin-android-tools/d17-0@a5194e9 Xamarin.iOS and Xamarin.Mac SDK 15.4.0.0 (8fc41ae82) Xamarin.iOS and Xamarin.Mac Reference Assemblies and MSBuild support. ```

Build Logs

Screenshots

Reproduction Link

TestApp.zip

Workaround

Removing the call to the Clear() Method did not trigger the bug. //TodoItems.Clear(); OR Not reinitializing the ObservableCollection but adding the items with the .Add() method works.

      //TodoItems = new ObservableCollection<TodoItem>(items);
      foreach (var todoItem in items)
      {
          TodoItems.Add(todoItem);
      }
jfversluis commented 2 years ago

Het @Gh0stC0de thanks for the report!

However, this works as intended, after you have created your initial ObservableCollection that has to stay intact in order for the data binding to work. If not, you will have to reset the BindingContext for it again.

Here in the warning box it is stated in the Docs: https://docs.microsoft.com/xamarin/xamarin-forms/user-interface/listview/data-and-databinding#binding-cells.

Although this is for ListView this is more applicable to using a ObservableCollection than the control it's used with.

Gh0stC0de commented 2 years ago

Thank you, @jfversluis , for your response!

I totally forgot the INotifiyPropertyChanged in the sample Project thats why the binding was not working after the new() statement. With the added interface everything works as expected. But if I then add an EmptyView to the Collection View the bug appears.

I defined the CollectionView as follows:

<CollectionView Grid.Row="1"
                ItemsSource="{Binding TodoItems}"
                EmptyView="Collection is null or empty.">
    <CollectionView.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="2*" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>

                <Label Grid.Column="0" Text="{Binding Name}" />
                <CheckBox Grid.Column="1" IsChecked="{Binding IsDone}" />
            </Grid>
        </DataTemplate>
    </CollectionView.ItemTemplate>
</CollectionView>

and the ViewModel looks like:

Show/Hide ViewModel ```csharp public class MainPageViewModel : INotifyPropertyChanged { private ObservableCollection todoItems; private int refreshCounter; public MainPageViewModel() { TodoItems = new ObservableCollection(); RefreshCommand = new Command(Refresh); EmptyCommand = new Command(Empty); Refresh(); } public ObservableCollection TodoItems { get => todoItems; set { if (value != todoItems) { todoItems = value; RaisePropertyChanged(); } } } public int RefreshCounter { get => refreshCounter; set { if (refreshCounter != value) { refreshCounter = value; RaisePropertyChanged(); } } } public Command RefreshCommand { get; set; } public Command EmptyCommand { get; set; } public event PropertyChangedEventHandler PropertyChanged; private void Refresh() { TodoItems.Clear(); var items = new[] { new TodoItem { Name = "Item 1" }, new TodoItem { Name = "Item 2" }, new TodoItem { Name = "Item 3" } }; TodoItems = new ObservableCollection(items); RefreshCounter++; } private void Empty() { TodoItems.Clear(); } private void RaisePropertyChanged([CallerMemberName] string propertyname = null) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyname)); } } } ```

I checked with the docs to make sure I'm using the EmptyView the correct way. Am I missing something?

Last known good version: 5.0.0.2244 Also: Removing the call to Clear() makes the refresh work but when pressing empty I then have to press refresh twice to make it work again. Seems wrong to me.

Here's the updated sample: TestApp.zip

jfversluis commented 2 years ago

I'm sorry but I'm still not sure what I'm supposed to see here. Running the sample and clicking the buttons seem to work fine for me

https://user-images.githubusercontent.com/939291/151375576-09a599b8-7fff-4ef2-9e23-3e15a4116f8c.mov

Gh0stC0de commented 2 years ago

Weired, when I'm running it on my end it looks like this:

https://user-images.githubusercontent.com/18737770/151381500-aa4595cd-30b4-4731-adc4-16ef3d0c43ed.mp4

jfversluis commented 2 years ago

Ok seems to be specific to Android then!

Gh0stC0de commented 2 years ago

Ok seems to be specific to Android then!

Can confirm, as I just managed to test it on iOS now.

MitchBomcanhao commented 1 year ago

this is still an issue with the current version, on android

Ulrizza commented 1 year ago

I only had the problem when the list was not initially visible. The only thing that worked for me was to force it to visible before changing its content.

ListIsVisible = true;
ListItems.Clear();
[Add or remove items from the list]
ListIsVisible = ListItems.Any();