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

Loaded event is fired for a removed ListView item #8652

Closed
ynborokh opened this issue Jul 17, 2023 · 1 comment
Closed

Loaded event is fired for a removed ListView item #8652

ynborokh opened this issue Jul 17, 2023 · 1 comment
Labels
bug Something isn't working team-Controls Issue for the Controls team

Comments

@ynborokh
Copy link

Describe the bug

Loaded event may be fired for an item that was already deleted from a ListView.

Steps to reproduce the bug

  1. Load the solution from this repo.
    If you inspect the solution code, you'll see the following. There is the ListView with custom ListView.ItemTemplate that uses Item control:
<ListView>
    <ListView.ItemTemplate>
        <DataTemplate x:DataType="x:String">
            <local:Item Id="{x:Bind}" />
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

If we look into Item control, we'll see the following:

<UserControl
    ...
    Loaded="OnLoaded"
    Unloaded="OnUnloaded">
    <Button Content="{x:Bind Id, Mode=OneWay}" />
</UserControl>

public sealed partial class Item : UserControl, INotifyPropertyChanged
{
    ...
    private void OnLoaded(object sender, Microsoft.UI.Xaml.RoutedEventArgs e)
    {
        Debug.WriteLine($"Loaded {((Item)sender).Id}");
    }

    private void OnUnloaded(object sender, Microsoft.UI.Xaml.RoutedEventArgs e)
    {
        Debug.WriteLine($"Unloaded {((Item)sender).Id}");
    }
}
  1. Start the application.
  2. Run DebugView or any other utility that captures debug output.
  3. Add some elements (say, 5) to the list using Add button. Every time you'll see the Loaded <id> message in DebugView. That's expected.
  4. Then start removing elements from the list using Remove button.
  5. You'll see that for the first removed item you'll see Unloaded <first_deleted_id> message that is expected. But after the deleting of the next item you'll see two messages:
Loaded <first_deleted_id>
Unloaded <second_deleted_id>

So for some reason Loaded event is called for the item that was just deleted. If I rely on Loaded-Unloaded event in my application logic (for example, for notifying a viewmodel about some UI changes or making some subscription-unsubscriptions), then I'll get into a trouble since Loaded event is called for an item that is deleted and is not in the list.

Expected behavior

Loaded event shouldn't be raised for a ListView item, that was already deleted and it not being added.

Screenshots

No response

NuGet package version

WinUI 3 - Windows App SDK 1.3.2: 1.3.230602002

Windows version

No response

Additional context

OS version is Win10 22HS 19045.3208
I can reproduce this problem also in 1.4.230628000-preview1 version of Windows App SDK.

@ynborokh ynborokh added the bug Something isn't working label Jul 17, 2023
@bpulliam bpulliam added the team-Controls Issue for the Controls team label Aug 9, 2023
@ranjeshj
Copy link
Contributor

ranjeshj commented Dec 1, 2023

Please don't depend on Loaded/Unloaded events. They are not guaranteed to happen in order today. See #1900

For ListView, please use the ContentContainerChanging event to setup/teardown when items come into view and get recycled.

@ranjeshj ranjeshj closed this as not planned Won't fix, can't repro, duplicate, stale Dec 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working team-Controls Issue for the Controls team
Projects
None yet
Development

No branches or pull requests

3 participants