-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainPage.xaml.cs
More file actions
220 lines (189 loc) · 8.39 KB
/
MainPage.xaml.cs
File metadata and controls
220 lines (189 loc) · 8.39 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.UI.Xaml.Media.Animation;
using System.Diagnostics;
using Windows.Storage;
using Windows.UI.Core.Preview;
using Windows.UI;
using Windows.ApplicationModel.Core;
using Windows.UI.ViewManagement;
using System;
using Windows.ApplicationModel.Resources;
using MeasurePixels.Controls;
using System.Threading.Tasks;
// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
namespace MeasurePixels
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
public static MainPage Current { get; private set; }
AppSettings settings;
public MainPage()
{
Current = this;
this.InitializeComponent();
settings = AppSettings.Current;
//添加关闭窗口监听
//需要在Package.appxmanifest中配置rescap
SystemNavigationManagerPreview.GetForCurrentView()
.CloseRequested += MainPage_CloseRequested;
this.UseCustomTitleBar();
Helpers.Toast.RegisterToastHandle(HandleToast);
}
private void Page_Loaded(object sender, RoutedEventArgs e)
{
this.contentFrame.Navigate(typeof(Pages.EditorPage));
}
private void GoBack_Click(object sender, RoutedEventArgs e)
{
this.contentFrame.GoBack();
}
private void Settings_Click(object sender, RoutedEventArgs e)
{
if (this.contentFrame.SourcePageType != typeof(Pages.SettingsPage))
{
this.contentFrame.Navigate(typeof(Pages.SettingsPage), "Settings",
new SlideNavigationTransitionInfo { Effect = SlideNavigationTransitionEffect.FromRight });
}
}
private void FullScreen_Click(object sender, RoutedEventArgs e)
{
var view = ApplicationView.GetForCurrentView();
if (view.IsFullScreenMode)
{
view.ExitFullScreenMode();
ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.Auto;
}
else
{
if (view.TryEnterFullScreenMode())
ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.FullScreen;
}
}
private async void contentFrame_NavigationFailed(object sender, NavigationFailedEventArgs e)
{
var resources = ResourceLoader.GetForCurrentView();
ContentDialog dialog = new ContentDialog
{
CloseButtonText = resources.GetString("Ok"),
Title = resources.GetString("NavigtionFailedTitle"),
Content = resources.GetString("NavigtionFailedContent")
};
Debug.WriteLine("Failed to load Page " + e.SourcePageType.FullName + ". "
+ Environment.NewLine + e.Exception.ToString());
await dialog.ShowAsync();
}
private void MainPage_CloseRequested(object sender, SystemNavigationCloseRequestedPreviewEventArgs e)
{
Debug.WriteLine("MainPage_CloseRequested");
}
#region TitleBar
private void UseCustomTitleBar()
{
var titleBar = ApplicationView.GetForCurrentView().TitleBar;
titleBar.ButtonBackgroundColor = Colors.Transparent;
titleBar.ButtonForegroundColor = GetForegroundColorByTheme();
titleBar.ButtonInactiveBackgroundColor = Colors.Transparent;
// Hide default title bar.
var coreTitleBar = CoreApplication.GetCurrentView().TitleBar;
coreTitleBar.ExtendViewIntoTitleBar = true;
UpdateTitleBarLayout(coreTitleBar);
// Set XAML element as a draggable region.
Window.Current.SetTitleBar(AppTitleBar);
// Register a handler for when the size of the overlaid caption control changes.
// For example, when the app moves to a screen with a different DPI.
coreTitleBar.LayoutMetricsChanged += CoreTitleBar_LayoutMetricsChanged;
// Register a handler for when the title bar visibility changes.
// For example, when the title bar is invoked in full screen mode.
coreTitleBar.IsVisibleChanged += CoreTitleBar_IsVisibleChanged;
//active state handle
//Window.Current.Activated += Current_Activated;
}
public Color GetForegroundColorByTheme(FrameworkElement sender = null)
{
sender = sender ?? this;
Color color = Colors.Black;
if (sender.RequestedTheme == ElementTheme.Dark
|| (sender.RequestedTheme == ElementTheme.Default
&& Application.Current.RequestedTheme == ApplicationTheme.Dark))
color = Colors.White;
return color;
}
private void Page_ActualThemeChanged(FrameworkElement sender, object args)
{
ApplicationView.GetForCurrentView().TitleBar.ButtonForegroundColor =
GetForegroundColorByTheme(sender);
}
//private void Current_Activated(object sender, Windows.UI.Core.WindowActivatedEventArgs e)
//{
// switch (e.WindowActivationState)
// {
// case Windows.UI.Core.CoreWindowActivationState.Deactivated:
// AppTitleBar.Background = new SolidColorBrush(Color.FromArgb(255, 230, 230, 230));
// break;
// case Windows.UI.Core.CoreWindowActivationState.PointerActivated:
// case Windows.UI.Core.CoreWindowActivationState.CodeActivated:
// default:
// AppTitleBar.Background = (Brush)App.Current.Resources["appbarAcrylicBrush"];
// break;
// }
//}
private void CoreTitleBar_LayoutMetricsChanged(CoreApplicationViewTitleBar sender, object args)
{
UpdateTitleBarLayout(sender);
}
private void UpdateTitleBarLayout(CoreApplicationViewTitleBar coreTitleBar)
{
// Get the size of the caption controls area and back button
// (returned in logical pixels), and move your content around as necessary.
LeftPaddingColumn.Width = new GridLength(coreTitleBar.SystemOverlayLeftInset);
RightPaddingColumn.Width = new GridLength(coreTitleBar.SystemOverlayRightInset);
// Update title bar control size as needed to account for system size changes.
AppTitleBar.Height = Math.Max(coreTitleBar.Height + 3, AppTitleBar.Height);
}
private void CoreTitleBar_IsVisibleChanged(CoreApplicationViewTitleBar sender, object args)
{
AppTitleBar.Visibility = sender.IsVisible ? Visibility.Visible : Visibility.Collapsed;
}
#endregion
public string Message
{
get { return (string)GetValue(MessageProperty); }
set { SetValue(MessageProperty, value); }
}
public static readonly DependencyProperty MessageProperty =
DependencyProperty.Register("Message", typeof(string), typeof(MainPage), new PropertyMetadata(string.Empty));
private DateTime _startTime;
private async void HandleToast(string message, int duration, Action callBack)
{
_startTime = DateTime.Now;
Message = message;
if (ToastMask.Visibility == Visibility.Visible)
return;
_ = Task.Run(async () =>
{
while (true)
{
if ((DateTime.Now - _startTime).TotalMilliseconds > duration)
break;
System.Threading.Thread.Sleep(100);
}
await Dispatcher.TryRunIdleAsync((_) => ToastMask.Visibility = Visibility.Collapsed);
callBack?.Invoke();
});
await Dispatcher.TryRunIdleAsync((_) => ToastMask.Visibility = Visibility.Visible);
}
}
}