microsoft / microsoft-ui-xaml

Windows UI Library: the latest Windows 10 native controls and Fluent styles for your applications
MIT License
6.36k stars 679 forks source link

In C++ `FindName` wont reload an object that has been `UnloadObject`ed #10179

Open tom-huntington opened 3 hours ago

tom-huntington commented 3 hours ago

Describe the bug

This doesn't happen in C#

This also happens if we replace the call to UnloadObject with myStackPanel.Children().ReplaceAll({});. This is similar to https://github.com/microsoft/microsoft-ui-xaml/issues/10176

Steps to reproduce the bug

<Window ...>
    <StackPanel x:Name="myStackPanel" Orientation="Vertical"  VerticalAlignment="Center">
            <Button x:Load="True" x:Name="myButton" Click="myButton_Click">Click Me</Button>
    </StackPanel>
</Window>
void MainWindow::myButton_Click(IInspectable const&, RoutedEventArgs const&)
{
    UnloadObject(myButton());
    winrt::check_bool(!myButton());
    auto r = myStackPanel().FindName(L"myButton");
    winrt::check_bool(!r); // wrong
    winrt::check_bool(!myButton()); // wrong
}
private void myButton_Click(object sender, RoutedEventArgs e)
{
    UnloadObject(myButton);
    Debug.Assert(myButton == null);
    var r = myStackPanel.FindName("myButton");
    Debug.Assert(r != null); // good
    Debug.Assert(myButton != null); // good
}

NuGet package version

WinUI 3 - Windows App SDK 1.6.3: 1.6.241114003

Windows version

Windows 11 (21H2): Build 22000

Additional context

No response

github-actions[bot] commented 3 hours ago

Hi I'm an AI powered bot that finds similar issues based off the issue title.

Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one. Thank you!

Open similar issues:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.

tom-huntington commented 1 hour ago

https://learn.microsoft.com/en-us/windows/uwp/xaml-platform/x-load-attribute

when the element is not loaded, its memory is released and internally a small placeholder is used to mark its place in the visual tree.

So I'm guessing this placeholder is being removed. When I iterate over Panel.Children the placeholders are not there. But calling UIElementCollection.ReplaceAll removes them, as per my other issue