microsoft / microsoft-ui-xaml

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

`XamlMarkupHelper.UnloadObject` gives exception #9819

Open HO-COOH opened 3 months ago

HO-COOH commented 3 months ago

Describe the bug

I am trying to unload a child control inside my templated control using Windows.UI.Xaml.Markup.XamlMarkupHelper.UnloadObject (UWP) and Microsoft.UI.Xaml.Markup.XamlMarkupHelper.UnloadObject (Winui3) as documented here. But I got exceptions:

WinRT originate error - 0x80070057 : 'The parameter is incorrect.'

Steps to reproduce the bug

  1. Create a new C++WinUI3 project (Packaged)
  2. Create a new templated control, add a DependencyProperty called IsLoading. And write code when IsLoading==false, call this api to unload a component.
  3. In MainWindow.xaml, add a ToggleSwitch and this new templated control, bind the IsLoading to the ToggleSwitch.IsOn

Expected behavior

No response

Screenshots

No response

NuGet package version

WinUI 3 - Windows App SDK 1.5.5: 1.5.240627000

Windows version

Windows 11 (22H2): Build 22621

Additional context

This issue happens on both UWP & WinUI3. If I am doing anything wrong, please update the documentation!

codendone commented 3 months ago

Do you have x:Load set on the element? I think that may be required in order to use XamlMarkupHelper.UnloadObject.

HO-COOH commented 3 months ago

Do you have x:Load set on the element? I think that may be required in order to use XamlMarkupHelper.UnloadObject.

Adding a x:Load="False" then loading it in code solves the issue (but please, add it in UnloadObject's remark section in the doc). This is from the UWP doc about x:Load.

The UI element attributed with x:Load can be loaded and unloaded via code, or using an x:Bind expression.

This says NOTHING about needing to set it to False then loaded in code, in order to be able to unload. Setting it to True you will still get the exceptions.

codendone commented 3 months ago

I agree the docs need to be clarified.