CppXAML
Loading...
Searching...
No Matches
XamlApplication.h
1#pragma once
2
3#include "CppXaml.XamlApplication.g.h"
4#include <winrt/Windows.UI.Xaml.Hosting.h>
5#include <appmodel.h>
6#include <strsafe.h>
7
8namespace winrt::CppXaml::implementation
9{
10 struct XamlApplication : XamlApplicationT<XamlApplication>
11 {
12 XamlApplication() = default;
13
14 XamlApplication(std::initializer_list<winrt::Windows::UI::Xaml::Markup::IXamlMetadataProvider> providers)
15 {
16 for (auto&& provider : providers)
17 {
18 m_providers.Append(provider);
19 }
20 }
21
22 ~XamlApplication()
23 {
24 Close();
25 }
26
27 void InitializeComponent() // C++ WinRT 2 phase construction
28 {
29 m_windowsXamlManager = winrt::Windows::UI::Xaml::Hosting::WindowsXamlManager::InitializeForCurrentThread();
30 }
31
32 winrt::Windows::Foundation::Collections::IVector<winrt::Windows::UI::Xaml::Markup::IXamlMetadataProvider> MetadataProviders()
33 {
34 return m_providers;
35 }
36
37 winrt::Windows::Foundation::IClosable WindowsXamlManager()
38 {
39 return m_windowsXamlManager;
40 }
41
42 // ICloseable
43 void Close()
44 {
45 if (b_closed) return;
46
47 b_closed = true;
48
49 m_windowsXamlManager.Close();
50 m_providers.Clear();
51 m_windowsXamlManager = nullptr;
52
53 // Toolkit has a message loop here.
54 }
55
56 // IXamlMetadataProvider
57 winrt::Windows::UI::Xaml::Markup::IXamlType GetXamlType(winrt::Windows::UI::Xaml::Interop::TypeName const& type)
58 {
59 for (const auto& provider : m_providers)
60 {
61 if (const auto xamlType = provider.GetXamlType(type))
62 {
63 return xamlType;
64 }
65 }
66 return nullptr;
67 }
68
69 winrt::Windows::UI::Xaml::Markup::IXamlType GetXamlType(hstring const& fullName)
70 {
71 for (const auto& provider : m_providers)
72 {
73 if (const auto xamlType = provider.GetXamlType(fullName))
74 {
75 return xamlType;
76 }
77 }
78 return nullptr;
79 }
80
81 com_array<winrt::Windows::UI::Xaml::Markup::XmlnsDefinition> GetXmlnsDefinitions()
82 {
83 std::list<winrt::Windows::UI::Xaml::Markup::XmlnsDefinition> definitions;
84 for (const auto& provider : m_providers)
85 {
86 auto defs = provider.GetXmlnsDefinitions();
87 for (const auto& def : defs)
88 {
89 definitions.insert(definitions.begin(), def);
90 }
91 }
92
93 return winrt::com_array<winrt::Windows::UI::Xaml::Markup::XmlnsDefinition>(definitions.begin(), definitions.end());
94 }
95
96 private:
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>();
100 bool b_closed{};
101 };
102}
103
104namespace winrt {
105 template<typename... TProviders>
106 winrt::CppXaml::XamlApplication make_application(TProviders&&... providers)
107 {
108 return winrt::make<winrt::CppXaml::implementation::XamlApplication>(std::initializer_list<winrt::Windows::UI::Xaml::Markup::IXamlMetadataProvider>{providers...});
109 }
110}
111
112#pragma comment(lib, "onecoreuap_apiset.lib")
113
114namespace cppxaml {
115 namespace details {
116 PWSTR g_winUIDepId{};
117 PACKAGEDEPENDENCY_CONTEXT g_ctx{};
118 }
119
126 void InitializeWinUI(uint8_t minorVersion = 8) {
127
128 if (details::g_winUIDepId == nullptr) {
129 PACKAGE_VERSION minVersion{};
130 minVersion.Major = minorVersion;
131
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));
135
136 winrt::check_hresult(TryCreatePackageDependency(nullptr, pfn, minVersion,
137 PackageDependencyProcessorArchitectures_None, PackageDependencyLifetimeKind_Process,
138 nullptr, CreatePackageDependencyOptions_None, &details::g_winUIDepId));
139 }
140
141 PWSTR packageFullName{};
142
143 winrt::check_hresult(AddPackageDependency(details::g_winUIDepId, 0, AddPackageDependencyOptions_None, &details::g_ctx, &packageFullName));
144 }
145
150 winrt::check_hresult(RemovePackageDependency(details::g_ctx));
151 // should HeapFree g_winUIDepId - but do it in a refcounted way
152 }
153}
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