3#include "CppXaml.XamlApplication.g.h"
4#include <winrt/Windows.UI.Xaml.Hosting.h>
8namespace winrt::CppXaml::implementation
10 struct XamlApplication : XamlApplicationT<XamlApplication>
12 XamlApplication() =
default;
14 XamlApplication(std::initializer_list<winrt::Windows::UI::Xaml::Markup::IXamlMetadataProvider> providers)
16 for (
auto&& provider : providers)
18 m_providers.Append(provider);
27 void InitializeComponent()
29 m_windowsXamlManager = winrt::Windows::UI::Xaml::Hosting::WindowsXamlManager::InitializeForCurrentThread();
32 winrt::Windows::Foundation::Collections::IVector<winrt::Windows::UI::Xaml::Markup::IXamlMetadataProvider> MetadataProviders()
37 winrt::Windows::Foundation::IClosable WindowsXamlManager()
39 return m_windowsXamlManager;
49 m_windowsXamlManager.Close();
51 m_windowsXamlManager =
nullptr;
57 winrt::Windows::UI::Xaml::Markup::IXamlType GetXamlType(winrt::Windows::UI::Xaml::Interop::TypeName
const& type)
59 for (
const auto& provider : m_providers)
61 if (
const auto xamlType = provider.GetXamlType(type))
69 winrt::Windows::UI::Xaml::Markup::IXamlType GetXamlType(hstring
const& fullName)
71 for (
const auto& provider : m_providers)
73 if (
const auto xamlType = provider.GetXamlType(fullName))
81 com_array<winrt::Windows::UI::Xaml::Markup::XmlnsDefinition> GetXmlnsDefinitions()
83 std::list<winrt::Windows::UI::Xaml::Markup::XmlnsDefinition> definitions;
84 for (
const auto& provider : m_providers)
86 auto defs = provider.GetXmlnsDefinitions();
87 for (
const auto& def : defs)
89 definitions.insert(definitions.begin(), def);
93 return winrt::com_array<winrt::Windows::UI::Xaml::Markup::XmlnsDefinition>(definitions.begin(), definitions.end());
97 winrt::Windows::UI::Xaml::Hosting::WindowsXamlManager m_windowsXamlManager{
nullptr };
98 winrt::Windows::Foundation::Collections::IVector<winrt::Windows::UI::Xaml::Markup::IXamlMetadataProvider> m_providers
99 = winrt::single_threaded_vector<Windows::UI::Xaml::Markup::IXamlMetadataProvider>();
105 template<
typename... TProviders>
106 winrt::CppXaml::XamlApplication make_application(TProviders&&... providers)
108 return winrt::make<winrt::CppXaml::implementation::XamlApplication>(std::initializer_list<winrt::Windows::UI::Xaml::Markup::IXamlMetadataProvider>{providers...});
112#pragma comment(lib, "onecoreuap_apiset.lib")
116 PWSTR g_winUIDepId{};
117 PACKAGEDEPENDENCY_CONTEXT g_ctx{};
128 if (details::g_winUIDepId ==
nullptr) {
129 PACKAGE_VERSION minVersion{};
130 minVersion.Major = minorVersion;
132 constexpr const std::wstring_view winuiPackageFamilyNameTemplate{ L
"Microsoft.UI.Xaml.2.%d_8wekyb3d8bbwe" };
133 wchar_t pfn[PACKAGE_FAMILY_NAME_MAX_LENGTH]{};
134 winrt::check_hresult(StringCchPrintf(pfn, std::size(pfn), winuiPackageFamilyNameTemplate.data(), minorVersion));
136 winrt::check_hresult(TryCreatePackageDependency(
nullptr, pfn, minVersion,
137 PackageDependencyProcessorArchitectures_None, PackageDependencyLifetimeKind_Process,
138 nullptr, CreatePackageDependencyOptions_None, &details::g_winUIDepId));
141 PWSTR packageFullName{};
143 winrt::check_hresult(AddPackageDependency(details::g_winUIDepId, 0, AddPackageDependencyOptions_None, &details::g_ctx, &packageFullName));
150 winrt::check_hresult(RemovePackageDependency(details::g_ctx));
The main CppXAML namespace.
void UninitializeWinUI()
Removes a reference to WinUI from the process's package graph.
Definition: XamlApplication.h:149
void InitializeWinUI(uint8_t minorVersion=8)
Dynamically initializes WinUI 2. This API works on Windows 11 and newer. Adds the WinUI 2 framework p...
Definition: XamlApplication.h:126