Maui.Markup icon indicating copy to clipboard operation
Maui.Markup copied to clipboard

[Bug] BindableLayout is not working - Making use of the ItemTemplate markup method

Open egvijayanand opened this issue 3 years ago • 4 comments

Description

BindableLayout is not working - Making use of the ItemTemplate markup method.

Stack Trace

Link to Reproduction Sample

https://github.com/egvijayanand/markup-issue-89

Steps to Reproduce

  1. Clone the repository
  2. Navigate to the src folder
  3. Open the LayoutApp solution
  4. Set Windows as the platform target, then build and run the project
  5. For testing, have provided both XAML and C# versions, switch to the appropriate start page in App.xaml.cs

Expected Behavior

A repeater kind of view that generates the view from the defined template for each of the items in the list.

Actual Behavior

No output. The screen is blank.

Basic Information

  • Version with issue: 1.0.1
  • Last known good version: NA
  • IDE: VS2022 17.3.0 Preview 4.0
  • Platform Target Frameworks:
    • iOS:
    • Android:
    • UWP/WinUI: net6.0-windows10.0.19041.0 - running on Windows 11
  • Android Support Library Version:
  • NuGet Packages:
  • Affected Devices:

Workaround

XAML version works as expected.

Reproduction imagery

egvijayanand avatar Jul 20 '22 23:07 egvijayanand

Thanks Vijay! Could you add a link to your reproduction sample?

TheCodeTraveler avatar Jul 20 '22 23:07 TheCodeTraveler

Working on, just give me a few more mins.

egvijayanand avatar Jul 20 '22 23:07 egvijayanand

Thanks for your patience. Here's the simulation source repo.

https://github.com/egvijayanand/markup-issue-89

egvijayanand avatar Jul 21 '22 00:07 egvijayanand

Another observation, there're only 4 items in the source collection, but it creates 5 blocks.

egvijayanand avatar Jul 21 '22 00:07 egvijayanand

Hey Vijay!

This is user error. You need to Bind to your ViewModel's Items property.

.ItemSource() just sets a static value for the BindableLayout.ItemsSourceProperty.

Interestingly, because .ItemSource() accepts an IEnumerable, it accepted your string, nameof(ManViewModel.Items), because it has an implicit cast to IEnumerable<char>.

Another observation, there're only 4 items in the source collection, but it creates 5 blocks

This happened because nameof(MainViewModel.Items) has 5 characters and thus BindableLayout created 5 DataTemplates:

IEnumerable<char> source = nameof(MainViewModel.Items); // source = "Items"
Debug.WriteLine(source.Count()); // Prints 5

Provided Sample Code

.ItemsSource(nameof(MainViewModel.Items))

Fixed Code

.Bind(BindableLayout.ItemsSourceProperty, nameof(MainViewModel.Items))
Screen Shot 2022-11-02 at 12 04 12 PM

TheCodeTraveler avatar Nov 02 '22 19:11 TheCodeTraveler

I don't plan to change anything in CommunityToolkit.Maui.Markup because string is a valid input for BindableLayout.ItemSource

TheCodeTraveler avatar Nov 02 '22 19:11 TheCodeTraveler

Yes, @brminnick.

Have seen this issue in another project in the recent past, this BindableLayout's ItemsSource() method is an exception as it expects the collection itself rather than the property name.

Thanks for your time in analyzing it.

egvijayanand avatar Nov 02 '22 19:11 egvijayanand