forked from ChrisGuzak/Win32XamlApp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Win32XamlApp.cpp
145 lines (117 loc) · 5.47 KB
/
Win32XamlApp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include "pch.h"
#include <win32app/XamlWin32Helpers.h>
#include <win32app/win32_app_helpers.h>
#include "XamlApplication.h"
inline constexpr auto contentText = LR"(
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006">
<NavigationView>
<StackPanel x:Name="StackPanel1"/>
<NavigationView.MenuItems>
<NavigationViewItem Content="Startup">
<NavigationViewItem.Icon>
<FontIcon Glyph="" />
</NavigationViewItem.Icon>
</NavigationViewItem>
<NavigationViewItem Content="Interaction">
<NavigationViewItem.Icon>
<FontIcon Glyph="" />
</NavigationViewItem.Icon>
</NavigationViewItem>
<NavigationViewItem Content="Appearance">
<NavigationViewItem.Icon>
<FontIcon Glyph="" />
</NavigationViewItem.Icon>
</NavigationViewItem>
<NavigationViewItem Content="Color schemes">
<NavigationViewItem.Icon>
<FontIcon Glyph="" />
</NavigationViewItem.Icon>
</NavigationViewItem>
<NavigationViewItem Content="Rendering">
<NavigationViewItem.Icon>
<FontIcon Glyph="" />
</NavigationViewItem.Icon>
</NavigationViewItem>
<NavigationViewItem Content="Actions">
<NavigationViewItem.Icon>
<FontIcon Glyph="" />
</NavigationViewItem.Icon>
</NavigationViewItem>
</NavigationView.MenuItems>
</NavigationView>
</Page>
)";
struct AppWindow
{
LRESULT Create()
{
// TODO: Proof of concept test of XamlApplication, work in progress.
// auto winuiIXMP = winrt::Microsoft::UI::Xaml::XamlTypeInfo::XamlControlsXamlMetaDataProvider();
// auto markupIXMP = winrt::AppMarkup::XamlMetaDataProvider();
// auto app = winrt::make_xaml_application(winuiIXMP, markupIXMP);
auto app = winrt::make_xaml_application();
m_xamlSource = winrt::Windows::UI::Xaml::Hosting::DesktopWindowXamlSource();
auto interop = m_xamlSource.as<IDesktopWindowXamlSourceNative>();
THROW_IF_FAILED(interop->AttachToWindow(m_window.get()));
THROW_IF_FAILED(interop->get_WindowHandle(&m_xamlSourceWindow));
// When this fails look in the debug output window, it shows the line and offset
// that has the parsing problem.
auto page = winrt::Windows::UI::Xaml::Markup::XamlReader::Load(contentText).as
<winrt::Windows::UI::Xaml::Controls::Page>();
auto navView = page.Content().as<winrt::Windows::UI::Xaml::Controls::NavigationView>();
m_itemInvokedRevoker = navView.ItemInvoked(winrt::auto_revoke, [this](auto&& sender, auto&& args)
{
auto item = args.InvokedItemContainer().as<winrt::Windows::UI::Xaml::Controls::NavigationViewItem>();
auto text = winrt::unbox_value<winrt::hstring>(item.Content());
auto tb = winrt::Windows::UI::Xaml::Controls::TextBlock();
tb.Text(L"Hello " + text);
auto stackPanel = sender.FindName(L"StackPanel1").as<winrt::Windows::UI::Xaml::Controls::StackPanel>();
stackPanel.Children().Append(tb);
});
m_pointerPressedRevoker = page.PointerPressed(winrt::auto_revoke, [this](auto&& sender, auto&& args)
{
auto pointerID = args.Pointer().PointerId();
auto tb = winrt::Windows::UI::Xaml::Controls::TextBlock();
tb.Text(L"PointerPressed");
auto page = sender.as<winrt::Windows::UI::Xaml::Controls::Page>();
auto stackPanel = page.FindName(L"StackPanel1").as<winrt::Windows::UI::Xaml::Controls::StackPanel>();
stackPanel.Children().Append(tb);
});
m_xamlSource.Content(page);
return 0;
}
LRESULT Size(WORD dx, WORD dy)
{
SetWindowPos(m_xamlSourceWindow, nullptr, 0, 0, dx, dy, SWP_SHOWWINDOW);
return 0;
}
LRESULT Destroy()
{
winrt::Windows::UI::Xaml::Application::Current().as<winrt::WinUIHelpers::XamlApplication>().Close();
// Since the xaml rundown is async and requires message dispatching,
// run it down here while the message loop is still running.
m_xamlSource.Close();
PostQuitMessage(0);
return 0;
}
void Show(int nCmdShow)
{
win32app::create_top_level_window_for_xaml(*this, L"Win32XamlAppWindow", L"Win32 Xaml App");
win32app::enter_simple_message_loop(*this, nCmdShow);
}
wil::unique_hwnd m_window;
HWND m_xamlSourceWindow{}; // This is owned by m_xamlSource, destroyed when Close() is called.
winrt::Windows::UI::Xaml::Hosting::DesktopWindowXamlSource m_xamlSource{ nullptr };
winrt::Windows::UI::Xaml::Controls::NavigationView::ItemInvoked_revoker m_itemInvokedRevoker;
winrt::Windows::UI::Xaml::UIElement::PointerPressed_revoker m_pointerPressedRevoker;
};
int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE, LPWSTR lpCmdLine, int nCmdShow)
{
auto coInit = wil::CoInitializeEx(COINIT_APARTMENTTHREADED);
std::make_unique<AppWindow>()->Show(nCmdShow);
return 0;
}