diff --git a/windows.ui.xaml.hosting/desktopwindowxamlsource.md b/windows.ui.xaml.hosting/desktopwindowxamlsource.md index d5410ddfcb..158fbd3a81 100644 --- a/windows.ui.xaml.hosting/desktopwindowxamlsource.md +++ b/windows.ui.xaml.hosting/desktopwindowxamlsource.md @@ -21,7 +21,7 @@ Equivalent WinUI class: [Microsoft.UI.Xaml.Hosting.DesktopWindowXamlSource](/win If you create a **DesktopWindowXamlSource** object before you create the [Windows.UI.Xaml.UIElement](../windows.ui.xaml/uielement.md) objects that will be hosted in it, the framework for hosting [Windows.UI.Xaml.UIElement](../windows.ui.xaml/uielement.md) content makes sure all the objects are initialized to the same thread. If you create the [Windows.UI.Xaml.UIElement](../windows.ui.xaml/uielement.md) objects before you create the **DesktopWindowXamlSource** object in which they will be hosted, you must call [WindowsXamlManager.InitializeForCurrentThread](windowsxamlmanager_initializeforcurrentthread_14911797.md) before you instantiate the [Windows.UI.Xaml.UIElement](../windows.ui.xaml/uielement.md) objects. -Because **DesktopWindowXamlSource** derives from [IClosable](../windows.foundation/iclosable.md), so it is recommended that you **Close** it (**Dispose** it in .NET) when you’re finished with it. +**DesktopWindowXamlSource** derives from [IClosable](../windows.foundation/iclosable.md). To avoid a leak that results from a cycle between it and **WindowsXamlManager** you must call **Close** (**Dispose** it in .NET) before you release **WindowsXamlManager**. ## -see-also [Using the UWP XAML hosting API in a desktop application](/windows/uwp/xaml-platform/using-the-xaml-hosting-api) diff --git a/windows.ui.xaml.hosting/windowsxamlmanager_initializeforcurrentthread_14911797.md b/windows.ui.xaml.hosting/windowsxamlmanager_initializeforcurrentthread_14911797.md index 285b38c61d..2d83026cb0 100644 --- a/windows.ui.xaml.hosting/windowsxamlmanager_initializeforcurrentthread_14911797.md +++ b/windows.ui.xaml.hosting/windowsxamlmanager_initializeforcurrentthread_14911797.md @@ -19,11 +19,25 @@ Equivalent WinUI method: [Microsoft.UI.Xaml.Hosting.WindowsXamlManager.Initializ An object that contains a reference to the UWP XAML framework. ## -remarks -Call this method to initialize the internal UWP XAML framework for the current thread in a desktop application in which you want to host [Windows.UI.Xaml.UIElement](../windows.ui.xaml/uielement.md) objects in a [DesktopWindowXamlSource](desktopwindowxamlsource.md). You only need to explicitly call this method if your application creates the **Windows.UI.Xaml.UIElement** objects before it creates the **DesktopWindowXamlSource** object that will host them. Your application should typically should call this method when the parent UI object that hosts the **DesktopWindowXamlSource** is instantiated. +Call this method to initialize the internal UWP XAML framework for the current thread in a desktop application in which you want to host [Windows.UI.Xaml.UIElement](../windows.ui.xaml/uielement.md) objects in a [DesktopWindowXamlSource](desktopwindowxamlsource.md). Call this method if your application creates the **Windows.UI.Xaml.UIElement** objects before it creates the **DesktopWindowXamlSource** object that will host them, or if your program uses XAML from more than one thread. -If you create a **DesktopWindowXamlSource** object before you create the **Windows.UI.Xaml.UIElement** objects that will be hosted in it, you don’t need to call this method. In this scenario, the UWP XAML framework will be initialized for you when you instantiate the **DesktopWindowXamlSource** object. +This method must be called before creating the **DesktopWindowXamlSource** on the thread. -This method returns a [WindowsXamlManager](windowsxamlmanager.md) object that contains a reference to the UWP XAML framework. You can create as many **WindowsXamlManager** objects as you want on a given thread. However, because each object holds a reference to the UWP XAML framework, you should **Close** (**Dispose** in .NET) the objects to ensure that XAML resources are eventually released. +This method returns a [WindowsXamlManager](windowsxamlmanager.md) object that contains a reference to the UWP XAML framework for the current thread. It is safe to call this method multiple times, it will return the same instance if one is active. +You must destroy the instance after destroying the **DesktopWindowXamlSource** instances. Note, after releasing this object a message loop must be run to finish the resource rundown, otherwise the associated resources will be leaked. + +```cpp + +// Break the cycle between the WindowsXamlManager and the DesktopWindowXamlSource. +m_xamlSource.Close(); +m_xamlManager = nullptr; + +// Drain the message queue after releasing WindowsXamlManager since rundown is async +while (PeekMessageW(&msg, nullptr, 0, 0, PM_REMOVE)) +{ + ::DispatchMessageW(&msg); +} +``` ## -see-also