-
Notifications
You must be signed in to change notification settings - Fork 619
/
ItemPage.xaml.cs
177 lines (156 loc) · 6.54 KB
/
ItemPage.xaml.cs
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//*********************************************************
//
// Copyright (c) Microsoft. All rights reserved.
// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************
using System;
using System.Linq;
using System.Numerics;
using System.Reflection;
using WinUIGallery.Data;
using WinUIGallery.Helper;
using Windows.Foundation;
using Windows.Foundation.Metadata;
using Windows.System;
using Microsoft.UI.Composition;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Hosting;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media.Animation;
using Microsoft.UI.Xaml.Navigation;
using WinUIGallery.DesktopWap.Controls;
namespace WinUIGallery
{
/// <summary>
/// A page that displays details for a single item within a group.
/// </summary>
public sealed partial class ItemPage : Page
{
private static string WinUIBaseUrl = "https://github.com/microsoft/microsoft-ui-xaml/tree/winui3/release/1.4-stable/controls/dev/";
private static string GalleryBaseUrl = "https://github.com/microsoft/WinUI-Gallery/tree/main/WinUIGallery/ControlPages/";
public ControlInfoDataItem Item
{
get { return _item; }
set { _item = value; }
}
private ControlInfoDataItem _item;
private ElementTheme? _currentElementTheme;
public ItemPage()
{
this.InitializeComponent();
Loaded += (s, e) => SetInitialVisuals();
}
public void SetInitialVisuals()
{
var navigationRootPage = NavigationRootPage.GetForElement(this);
if (navigationRootPage != null)
{
pageHeader.ToggleThemeAction = OnToggleTheme;
navigationRootPage.NavigationViewLoaded = OnNavigationViewLoaded;
this.Focus(FocusState.Programmatic);
}
if (UIHelper.IsScreenshotMode)
{
var controlExamples = (this.contentFrame.Content as UIElement)?.GetDescendantsOfType<ControlExample>();
if (controlExamples != null)
{
foreach (var controlExample in controlExamples)
{
VisualStateManager.GoToState(controlExample, "ScreenshotMode", false);
}
}
}
}
private void OnNavigationViewLoaded()
{
NavigationRootPage.GetForElement(this).EnsureNavigationSelection(this.Item.UniqueId);
}
protected override async void OnNavigatedTo(NavigationEventArgs e)
{
NavigationRootPageArgs args = (NavigationRootPageArgs)e.Parameter;
var uniqueId = (string)args.Parameter;
var group = await ControlInfoDataSource.GetGroupFromItemAsync(uniqueId);
var item = group?.Items.FirstOrDefault(x => x.UniqueId.Equals(uniqueId));
if (item != null)
{
Item = item;
// Load control page into frame.
Type pageType = Type.GetType("WinUIGallery.ControlPages." + item.UniqueId + "Page");
if (pageType != null)
{
var pageName = string.IsNullOrEmpty(group.Folder) ? pageType.Name : $"{group.Folder}/{pageType.Name}";
pageHeader.SetControlSourceLink(WinUIBaseUrl, item.SourcePath);
pageHeader.SetSamplePageSourceLinks(GalleryBaseUrl, pageName);
System.Diagnostics.Debug.WriteLine(string.Format("[ItemPage] Navigate to {0}", pageType.ToString()));
this.contentFrame.Navigate(pageType);
}
args.NavigationRootPage.EnsureNavigationSelection(item?.UniqueId);
}
base.OnNavigatedTo(e);
}
protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
SetControlExamplesTheme(ThemeHelper.ActualTheme);
base.OnNavigatingFrom(e);
}
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
var navigationRootPage = NavigationRootPage.GetForElement(this);
if (navigationRootPage != null)
{
navigationRootPage.NavigationViewLoaded = null;
pageHeader.ToggleThemeAction = null;
pageHeader.CopyLinkAction = null;
}
// We use reflection to call the OnNavigatedFrom function the user leaves this page
// See this PR for more information: https://github.com/microsoft/WinUI-Gallery/pull/145
Frame contentFrameAsFrame = contentFrame as Frame;
Page innerPage = contentFrameAsFrame.Content as Page;
if (innerPage != null)
{
MethodInfo dynMethod = innerPage.GetType().GetMethod("OnNavigatedFrom",
BindingFlags.NonPublic | BindingFlags.Instance);
dynMethod.Invoke(innerPage, new object[] { e });
}
base.OnNavigatedFrom(e);
}
public static ItemPage GetForElement(object obj)
{
UIElement element = (UIElement)obj;
Window window = WindowHelper.GetWindowForElement(element);
if (window != null)
{
return (ItemPage)window.Content;
}
return null;
}
private void OnToggleTheme()
{
var currentElementTheme = ((_currentElementTheme ?? ElementTheme.Default) == ElementTheme.Default) ? ThemeHelper.ActualTheme : _currentElementTheme.Value;
var newTheme = currentElementTheme == ElementTheme.Dark ? ElementTheme.Light : ElementTheme.Dark;
SetControlExamplesTheme(newTheme);
}
private void SetControlExamplesTheme(ElementTheme theme)
{
var controlExamples = (this.contentFrame.Content as UIElement)?.GetDescendantsOfType<SampleThemeListener>();
if (controlExamples != null)
{
_currentElementTheme = theme;
foreach (var controlExample in controlExamples)
{
controlExample.RequestedTheme = theme;
}
if(controlExamples.Count() == 0)
{
this.RequestedTheme = theme;
}
}
}
}
}