CppXAML
Loading...
Searching...
No Matches
InitializeWithWindow.h
Go to the documentation of this file.
1#pragma once
2#include <cppxaml/Controls.h>
4#include <type_traits>
5#include <ShObjIdl_core.h>
6#include <winrt/Windows.UI.Xaml.Controls.Primitives.h>
15namespace cppxaml {
16
17 auto SetXamlRoot(winrt::Windows::Foundation::IInspectable obj, cppxaml::xaml::XamlRoot root) {
18 if (auto fe = obj.try_as<winrt::Windows::UI::Xaml::FrameworkElement>()) {
19 return fe.XamlRoot(root);
20 }
21 else if (auto fb = obj.try_as<winrt::Windows::UI::Xaml::Controls::Primitives::FlyoutBase>()) {
22 return fb.XamlRoot(root);
23 }
24 throw winrt::hresult_error(E_NOINTERFACE);
25 }
26
36 template<typename TWindow>
37#ifndef DOXY
38 std::enable_if_t<
39 !std::is_same_v<TWindow, cppxaml::XamlWindow*> &&
40 !std::is_same_v<TWindow, HWND>>
41#else
42 void
43#endif
44 InitializeWithWindow(winrt::Windows::Foundation::IInspectable obj, TWindow /*winrt::Microsoft::UI::Xaml::Window*/ w) {
45#ifdef USE_WINUI3
46 if (auto iww = obj.try_as<IInitializeWithWindow>()) {
47 HWND hwnd{};
48 w.as<IWindowNative>()->get_WindowHandle(&hwnd);
49 iww->Initialize(hwnd);
50 }
51 else if (auto window = w.as<cppxaml::xaml::Window>()) {
52 obj.XamlRoot(window.Content().XamlRoot());
53 }
54 else
55#else
56 {
57 SetXamlRoot(obj, w.XamlRoot());
58 }
59#endif
60 }
61
68 bool InitializeWithWindow(winrt::Windows::Foundation::IInspectable obj, HWND hwnd) {
69 if (auto iww = obj.try_as<IInitializeWithWindow>()) {
70 iww->Initialize(hwnd);
71 return true;
72 }
73 return false;
74 }
75
81 void InitializeWithWindow(winrt::Windows::Foundation::IInspectable obj, const cppxaml::XamlWindow* xw) {
82 if (!InitializeWithWindow(obj, xw->hwnd())) {
83 SetXamlRoot(obj, xw->GetUIElement<cppxaml::xaml::FrameworkElement>().XamlRoot());
84 }
85 }
86
95 template<typename TUIElement, typename TWindow>
96 #ifndef DOXY
97 std::enable_if_t<std::is_assignable_v<cppxaml::xaml::UIElement, TUIElement> &&
98 !std::is_same_v<TWindow, const cppxaml::XamlWindow*>, void>
99#else
100 void
101#endif
102 InitializeWithWindow(cppxaml::details::Wrapper<TUIElement> obj, TWindow /*winrt::Microsoft::UI::Xaml::Window*/ w) {
103 return InitializeWithWindow(*obj, w);
104 }
105}
The main CppXAML namespace.
void InitializeWithWindow(winrt::Windows::Foundation::IInspectable obj, TWindow w)
Initializes an object with a window-like object.
Definition: InitializeWithWindow.h:44
Implements an HWND based host for XAML Islands. You can create a XamlWindow from one of three overloa...
Definition: XamlWindow.h:54
HWND hwnd() const
returns the HWND backing the XamlWindow.
Definition: XamlWindow.h:87
T GetUIElement() const
returns the XAML UI that the XamlWindow was created with.
Definition: XamlWindow.h:97