I'm trying to use XAML Islands for WinUI 3 in a Win32 application.
After creating a new Win32 Desktop application from the File -> New Projet wizard, and:
<WindowsPackageType>None</WindowsPackageType>
to the <PropertyGroup Label="Globals">
section of project file, and#include "framework.h"
#include "WinUI3XamlIslandsWin32.h"
#include <windows.ui.xaml.hosting.desktopwindowxamlsource.h>
#include <winrt/Microsoft.UI.Dispatching.h>
#include <winrt/Microsoft.UI.Xaml.Hosting.h>
#include <cassert>
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
winrt::init_apartment(winrt::apartment_type::single_threaded);
auto dispatcherQueueController = winrt::Microsoft::UI::Dispatching::DispatcherQueueController::CreateOnCurrentThread();
auto windowsXamlManager = winrt::Microsoft::UI::Xaml::Hosting::WindowsXamlManager::InitializeForCurrentThread();
winrt::Microsoft::UI::Xaml::Hosting::DesktopWindowXamlSource desktopWindowXamlSource;
auto interop = desktopWindowXamlSource.as<IDesktopWindowXamlSourceNative>();
assert(interop != nullptr);
return EXIT_SUCCESS;
}
If you run this program, it can successfully create the DesktopWindowXamlSource
, but the line desktopWindowXamlSource.as<IDesktopWindowXamlSourceNative>()
fails with E_NOINTERFACE
. Indeed, if you try winrt::get_interfaces(desktopWindowXamlSource)
to list the interfaces the DesktopWindowXamlSource
supports, it only lists:
IDesktopWindowXamlSource
ICompositionSupportsSystemBackdrop
IClosable
IWeakReferenceSource
ISupportErrorInfo
IUnknown
IInspectable
Is there some extra step of setup necessary to allow the DesktopWindowXamlSource
to be casted to the IDesktopWindowXamlSourceNative
interface?
I've created a sample project at https://github.com/litherum/WinUI3XamlIslandsWin32 demonstrating the problem.
It looks like WinUI 3 changed how XAML Islands work.
You can convert between HWND
s and WindowID
s by GetWindowFromWindowId()
and GetWindowIdFromWindow()
.