Using XAML Islands in a Win32 Project, using the most basic example from MS-Learn, WindowsXamlManager::InitializeForCurrentThread() throws a fatal exception.

Atul Majithia 20 Reputation points
2024-07-25T18:48:33.9166667+00:00

The following code from an MS-Learn page about XAML-Islands, crashes at the call to:

WindowsXamlManager winxamlmanager = WindowsXamlManager::InitializeForCurrentThread();

I have added the following two NuGet packages to the App:

Microsoft.Toolkit.Win32.UI.SDK (6.1.2)

Microsoft.Windows.CppWinRT (2.0.240405.15)

Also, added an app.manifest.xml file, as per the instructions in the example code.

Link to page of the example:

https://learn.microsoft.com/en-us/windows/apps/desktop/modernize/host-standard-control-with-xaml-islands-cpp

I am new to this; any insight would be much appreciated.

// Code

// WindowsProject3.cpp : Defines the entry point for the application.

//

#include "framework.h"

#include "WindowsProject3.h"

#include <string.h>

#include <winrt/Windows.Foundation.Collections.h>

#include <winrt/Windows.system.h>

#include <winrt/windows.ui.xaml.hosting.h>

#include <windows.ui.xaml.hosting.desktopwindowxamlsource.h>

#include <winrt/windows.ui.xaml.controls.h>

#include <winrt/Windows.ui.xaml.media.h>

#ifdef GetCurrentTime

#undef GetCurrentTime

#endif

using namespace winrt;

using namespace Windows::UI;

using namespace Windows::UI::Composition;

using namespace Windows::UI::Xaml::Hosting;

using namespace Windows::Foundation::Numerics;

LRESULT CALLBACK WindowProc(HWND, UINT, WPARAM, LPARAM);

HWND _hWnd;

HWND _childhWnd;

HINSTANCE _hInstance;

int CALLBACK WinMain(In HINSTANCE hInstance, In_opt HINSTANCE hPrevInstance, In LPSTR lpCmdLine, In int nCmdShow)

{

// The call to winrt::init_apartment initializes COM; by default, in a multithreaded apartment.

winrt::init_apartment(apartment_type::single_threaded);



_hInstance = hInstance;

// The main window class name.

const wchar_t szWindowClass[] = L"Win32DesktopApp";

WNDCLASSEX windowClass = { };

windowClass.cbSize = sizeof(WNDCLASSEX);

windowClass.lpfnWndProc = WindowProc;

windowClass.hInstance = hInstance;

windowClass.lpszClassName = szWindowClass;

windowClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);

windowClass.hIconSm = LoadIcon(windowClass.hInstance, IDI_APPLICATION);

if (RegisterClassEx(&windowClass) == NULL)

{

    MessageBox(NULL, L"Windows registration failed!", L"Error", NULL);

    return 0;

}

_hWnd = CreateWindow(

    szWindowClass,

    L"Windows c++ Win32 Desktop App",

    WS_OVERLAPPEDWINDOW | WS_VISIBLE,

    CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,

    NULL,

    NULL,

    hInstance,

    NULL

);

if (_hWnd == NULL)

{

    MessageBox(NULL, L"Call to CreateWindow failed!", L"Error", NULL);

    return 0;

}

// Begin XAML Island section.

// The call to winrt::init_apartment initializes COM; by default, in a multithreaded apartment.

//winrt::init_apartment(apartment_type::single_threaded);

// Initialize the XAML framework's core window for the current thread.

WindowsXamlManager winxamlmanager = WindowsXamlManager::InitializeForCurrentThread();

// This DesktopWindowXamlSource is the object that enables a non-UWP desktop application 

// to host WinRT XAML controls in any UI element that is associated with a window handle (HWND).

DesktopWindowXamlSource desktopSource;

// Get handle to the core window.

auto interop = desktopSource.as<IDesktopWindowXamlSourceNative>();

// Parent the DesktopWindowXamlSource object to the current window.

check_hresult(interop->AttachToWindow(_hWnd));

// This HWND will be the window handler for the XAML Island: A child window that contains XAML.  

HWND hWndXamlIsland = nullptr;

// Get the new child window's HWND. 

interop->get_WindowHandle(&hWndXamlIsland);

// Update the XAML Island window size because initially it is 0,0.

SetWindowPos(hWndXamlIsland, 0, 200, 100, 800, 200, SWP_SHOWWINDOW);

// Create the XAML content.

Windows::UI::Xaml::Controls::StackPanel xamlContainer;

xamlContainer.Background(Windows::UI::Xaml::Media::SolidColorBrush{ Windows::UI::Colors::LightGray() });

Windows::UI::Xaml::Controls::TextBlock tb;

tb.Text(L"Hello World from Xaml Islands!");

tb.VerticalAlignment(Windows::UI::Xaml::VerticalAlignment::Center);

tb.HorizontalAlignment(Windows::UI::Xaml::HorizontalAlignment::Center);

tb.FontSize(48);

xamlContainer.Children().Append(tb);

xamlContainer.UpdateLayout();

desktopSource.Content(xamlContainer);

// End XAML Island section.

ShowWindow(_hWnd, nCmdShow);

UpdateWindow(_hWnd);

//Message loop:

MSG msg = { };

while (GetMessage(&msg, NULL, 0, 0))

{

    TranslateMessage(&msg);

    DispatchMessage(&msg);

}

return 0;

}

Windows App SDK
Windows App SDK
A set of Microsoft open-source libraries, frameworks, components, and tools to be used in apps to access Windows platform functionality on many versions of Windows. Previously known as Project Reunion.
794 questions
Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,651 questions
{count} votes

Accepted answer
  1. RLWA32 45,691 Reputation points
    2024-07-26T00:09:25.32+00:00

    You may have renamed app.manifest.xml to app.manifest but there is probably one more change to make. Open the property page for app.manifest

    If it looks like this -

    app_xml

    You need to change the Item Type to Manifest Tool as shown here -

    app_manifest

    Then do a complete rebuild.

    2 people found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Atul Majithia 20 Reputation points
    2024-07-26T12:16:23.18+00:00

    The fix, thanks to RLWA32, was to change the app.manifest file's property "Item Type" from "Xml" to "Manifest Tool".


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.