Skip to content
2 changes: 1 addition & 1 deletion windows.ui.xaml.hosting/desktopwindowxamlsource.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,22 @@ 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 your program uses more than one thread that uses Xaml. Call this method before creating the **DesktopWindowXamlSource**.

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 returns a [WindowsXamlManager](windowsxamlmanager.md) object that contains a reference to the UWP XAML framework. Call this method as many times as you want but generally get one instance and run it down after running down 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.

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.
```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))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

while (PeekMessageW(&msg, nullptr, 0, 0, PM_REMOVE))

{
::DispatchMessageW(&msg);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

    ::DispatchMessageW(&msg);

}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

```

## -see-also

Expand Down