Skip to content

ItemsRepeater element recycling has bug for ItemsSource index 0 #10905

@E-K

Description

@E-K

Describe the bug

Using ItemsRepeater with LinedFlowLayout and checking recycling behaviour by ElementPrepared and ElementClearing event.
Two deferent element bound for index 0 of ItemsSource.

log

ElementPrepared Index:0 Ptr:3211120194192
ElementPrepared Index:0 Ptr:3211120194544
ElementPrepared Index:1 Ptr:3211120197360
ElementPrepared Index:2 Ptr:3211120199472
ElementPrepared Index:3 Ptr:3211120195952
ElementPrepared Index:4 Ptr:3211120195248
ElementPrepared Index:5 Ptr:3211120192432
ElementPrepared Index:6 Ptr:3211120196304
ElementPrepared Index:7 Ptr:3211120197008
ElementPrepared Index:8 Ptr:3211120197712
ElementPrepared Index:9 Ptr:3211120193136
<ScrollView Loaded="ScrollView_Loaded">
  <ItemsRepeater x:Name="itemsRepeater"
                 ElementPrepared="itemsRepeater_ElementPrepared"
                 ElementClearing="itemsRepeater_ElementClearing">
    <ItemsRepeater.ItemTemplate>
      <DataTemplate x:DataType="x:String">
        <TextBlock Text="{x:Bind}" Width="100" Height="100" />
      </DataTemplate>
    </ItemsRepeater.ItemTemplate>
    <ItemsRepeater.Layout>
      <LinedFlowLayout />
    </ItemsRepeater.Layout>
  </ItemsRepeater>
</ScrollView>
private void itemsRepeater_ElementPrepared(ItemsRepeater sender, ItemsRepeaterElementPreparedEventArgs args)
{
    nint ptr = ((IWinRTObject)args.Element).NativeObject.ThisPtr;
    Debug.WriteLine($"ElementPrepared Index:{args.Index} Ptr:{ptr.ToInt64()}");
}

private void itemsRepeater_ElementClearing(ItemsRepeater sender, ItemsRepeaterElementClearingEventArgs args)
{
    nint ptr = ((IWinRTObject)args.Element).NativeObject.ThisPtr;
    Debug.WriteLine($"ElementClearing Ptr:{ptr.ToInt64()}");
}
Image

Next, scrolling down, down, down.., Two elements for Index 0 repeats Prepared and Clearing.

ElementPrepared Index:0 Ptr:3211131401264
ElementPrepared Index:0 Ptr:3211131409360
ElementClearing Ptr:3211131401264
ElementPrepared Index:0 Ptr:3211131401264
ElementClearing Ptr:3211131409360
ElementPrepared Index:0 Ptr:3211131409360
ElementClearing Ptr:3211131401264
ElementPrepared Index:0 Ptr:3211131401264
ElementClearing Ptr:3211131409360
ElementPrepared Index:0 Ptr:3211131409360
ElementClearing Ptr:3211131401264
ElementPrepared Index:0 Ptr:3211131401264
ElementClearing Ptr:3211131409360
ElementPrepared Index:0 Ptr:3211131409360
ElementClearing Ptr:3211131401264
ElementPrepared Index:0 Ptr:3211131401264
ElementClearing Ptr:3211131409360
ElementPrepared Index:0 Ptr:3211131409360
ElementClearing Ptr:3211131401264
ElementPrepared Index:0 Ptr:3211131401264

Why is this important?

For apps like microsoft photo, ItemsRepeater's ElementPrepared and ElementClearing event timing is very important for managing thumbnail image.

Steps to reproduce the bug

Repro

ItemsRepeaterBug01Repro.zip

  1. Start Debug and Scroll down, down, down.
  2. See Visual Studio Debug Output.

Actual behavior

  • Two Element bound for Index 0.
  • Two elements for Index 0 repeats Prepared and Clearing.

Expected behavior

Element for Index 0 behaves like for Index 1. Bound Single Item and dont repeat Prepared and Clearing.

Screenshots

No response

NuGet package version

WinUI 3 - Windows App SDK 1.8.3: 1.8.251106002

Windows version

Windows 11 (24H2): Build 26100

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    Status

    Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions