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;
}