-
Notifications
You must be signed in to change notification settings - Fork 1
/
App.xaml.cs
770 lines (699 loc) · 34.3 KB
/
App.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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using Microsoft.UI.Windowing;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Media.Imaging;
using Microsoft.UI.Xaml.Navigation;
using Microsoft.UI.Xaml.Shapes;
using Windows.ApplicationModel.Activation;
using Windows.Foundation;
using Windows.UI.Popups;
namespace Draggable;
/// <summary>
/// Provides application-specific behavior to supplement the default Application class.
/// </summary>
public partial class App : Application
{
#region [Properties]
Window? m_window;
int m_width = 245;
int m_height = 270; // including titlebar
static bool _lastSave = false;
static DateTime _lastMove = DateTime.Now;
static DateTime _lastSize = DateTime.Now;
static Config? _localConfig;
public static bool IsClosing { get; set; } = false;
public static IntPtr WindowHandle { get; set; }
public static FrameworkElement? MainRoot { get; set; }
public static event Action<Windows.Graphics.SizeInt32>? OnWindowSizeChanged = (size) => { };
public static Config? LocalConfig
{
get => _localConfig;
set => _localConfig = value;
}
public static Func<Config?, bool> SaveConfigFunc = (cfg) =>
{
if (cfg is not null)
{
cfg.version = $"{GetCurrentAssemblyVersion()}";
if (cfg.opacity == 0.0) { cfg.opacity = 0.1; }
Process proc = Process.GetCurrentProcess();
cfg.metrics = $"Process used {proc.PrivateMemorySize64 / 1024 / 1024}MB of memory and {proc.TotalProcessorTime.ToReadableString()} TotalProcessorTime on {Environment.ProcessorCount} possible cores.";
try
{
_ = ConfigHelper.SaveConfig(cfg);
return true;
}
catch (Exception) { return false; }
}
return false;
};
// https://learn.microsoft.com/en-us/windows/apps/package-and-deploy/#advantages-and-disadvantages-of-packaging-your-app
#if IS_UNPACKAGED // We're using a custom PropertyGroup Condition we defined in the csproj to help us with the decision.
public static bool IsPackaged { get => false; }
#else
public static bool IsPackaged { get => true; }
#endif
#endregion
/// <summary>
/// Initializes the singleton application object. This is the first line of authored code
/// executed, and as such is the logical equivalent of main() or WinMain().
/// </summary>
public App()
{
Debug.WriteLine($"[INFO] {System.Reflection.MethodBase.GetCurrentMethod()?.DeclaringType?.Name}__{System.Reflection.MethodBase.GetCurrentMethod()?.Name} [{DateTime.Now.ToString("hh:mm:ss.fff tt")}]");
App.Current.DebugSettings.FailFastOnErrors = false;
#region [Exception handlers]
AppDomain.CurrentDomain.UnhandledException += CurrentDomainUnhandledException;
AppDomain.CurrentDomain.FirstChanceException += CurrentDomainFirstChanceException;
AppDomain.CurrentDomain.ProcessExit += CurrentDomainOnProcessExit;
TaskScheduler.UnobservedTaskException += OnUnobservedTaskException;
UnhandledException += ApplicationUnhandledException;
#endregion
this.InitializeComponent();
foreach (var ra in GetReferencedAssemblies())
{
Debug.WriteLine($"[INFO] {ra.Key} {ra.Value}");
}
}
/// <summary>
/// Invoked when the application is launched.
/// </summary>
/// <param name="args">Details about the launch request and process.</param>
protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
{
#region [Config]
if (ConfigHelper.DoesConfigExist())
{
try
{
LocalConfig = ConfigHelper.LoadConfig();
// If we started from the registry, then set the current directory to the application's directory.
if (LocalConfig != null && !IsPackaged)
{
var exePath = Assembly.GetExecutingAssembly().Location;
var exeDirectory = System.IO.Path.GetDirectoryName(exePath);
if (!string.IsNullOrEmpty(exeDirectory))
{
Directory.SetCurrentDirectory(exeDirectory);
}
}
}
catch (Exception ex)
{
DebugLog($"[ERROR] {nameof(ConfigHelper.LoadConfig)}: {ex.Message}");
}
}
else
{
try
{
LocalConfig = new Config
{
version = $"{App.GetCurrentAssemblyVersion()}",
metrics = "N/A",
opacity = 0.6,
gradientLength = 0.5,
gradientDarken = true,
windowW = m_width,
windowH = m_height,
clockFace = "Clockface1c",
randomHands = false,
hideTaskbar = false,
hourColor = "4169E1", // blue
minuteColor = "404040", // dark gray
secondColor = "B22222" // red
};
ConfigHelper.SaveConfig(LocalConfig);
}
catch (Exception ex)
{
DebugLog($"[ERROR] {nameof(ConfigHelper.SaveConfig)}: {ex.Message}");
}
}
#endregion
#region [AppActivationArguments]
var appInst = Microsoft.Windows.AppLifecycle.AppInstance.GetCurrent();
if (appInst != null)
{
var activationArguments = appInst.GetActivatedEventArgs();
DebugLog($"Activation kind: {activationArguments.Kind}");
if(activationArguments.Data is Windows.ApplicationModel.Activation.IToastNotificationActivatedEventArgs tnaea)
Debug.WriteLine($"[INFO] IToastNotificationActivatedEventArgs: {tnaea.UserInput.Count}");
if (activationArguments.Data is Windows.ApplicationModel.Activation.IStartupTaskActivatedEventArgs staea)
Debug.WriteLine($"[INFO] IStartupTaskActivatedEventArgs: {staea.TaskId}");
if (activationArguments.Data is Windows.ApplicationModel.Activation.IBackgroundActivatedEventArgs baea)
Debug.WriteLine($"[INFO] IBackgroundActivatedEventArgs: {baea.TaskInstance.SuspendedCount}");
if (activationArguments.Data is Windows.ApplicationModel.Activation.IApplicationViewActivatedEventArgs avaea)
Debug.WriteLine($"[INFO] IApplicationViewActivatedEventArgs: {avaea.CurrentlyShownApplicationViewId}");
if (activationArguments.Data is Windows.ApplicationModel.Activation.IActivatedEventArgsWithUser aeawu)
Debug.WriteLine($"[INFO] IActivatedEventArgsWithUser: {aeawu.User.Type}");
if (activationArguments.Data is Windows.ApplicationModel.Activation.IDeviceActivatedEventArgs daea)
Debug.WriteLine($"[INFO] IDeviceActivatedEventArgs: {daea.DeviceInformationId}");
if (activationArguments.Data is Windows.ApplicationModel.Activation.IFileActivatedEventArgs faea)
Debug.WriteLine($"[INFO] IFileActivatedEventArgs: {faea.Files.Count}");
if (activationArguments.Data is Windows.ApplicationModel.Activation.ISearchActivatedEventArgs saea)
Debug.WriteLine($"[INFO] ISearchActivatedEventArgs: {saea.QueryText}");
if (activationArguments.Data is Windows.ApplicationModel.Activation.ILaunchActivatedEventArgs laea)
{
if (laea.PreviousExecutionState == ApplicationExecutionState.NotRunning || laea.PreviousExecutionState == ApplicationExecutionState.Terminated)
DebugLog($"The app did not close normally: {laea.PreviousExecutionState}");
else
DebugLog($"The app closed normally: {laea.PreviousExecutionState}");
}
else if (activationArguments.Data is Windows.ApplicationModel.Activation.IActivatedEventArgs aea)
{
if (aea.PreviousExecutionState == ApplicationExecutionState.NotRunning || aea.PreviousExecutionState == ApplicationExecutionState.Terminated)
DebugLog($"The app did not close normally: {aea.PreviousExecutionState}");
else
DebugLog($"The app closed normally: {aea.PreviousExecutionState}");
}
}
#endregion
m_window = new MainWindow();
var appWin = GetAppWindow(m_window);
if (appWin != null)
{
// Gets or sets a value that indicates whether this window will appear in various system representations, such as ALT+TAB and taskbar.
if (LocalConfig!.hideTaskbar)
{
appWin.IsShownInSwitchers = false;
appWin.TitleBar.IconShowOptions = IconShowOptions.HideIconAndSystemMenu;
}
else
{
appWin.IsShownInSwitchers = true;
appWin.TitleBar.IconShowOptions = IconShowOptions.ShowIconAndSystemMenu;
}
// We don't have the Closing event exposed by default, so we'll use the AppWindow to compensate.
appWin.Closing += (s, e) =>
{
App.IsClosing = true;
Debug.WriteLine($"[INFO] Application closing detected at {DateTime.Now.ToString("hh:mm:ss.fff tt")}");
_lastSave = SaveConfigFunc(LocalConfig);
};
// Destroying is always called, but Closing is only called when the application is shutdown normally.
appWin.Destroying += (s, e) =>
{
Debug.WriteLine($"[INFO] Application destroying detected at {DateTime.Now.ToString("hh:mm:ss.fff tt")}");
if (!_lastSave) // prevent redundant calls
SaveConfigFunc(LocalConfig);
};
// The changed event holds a bunch of juicy info that we can extrapolate.
appWin.Changed += (s, args) =>
{
if (args.DidSizeChange)
{
Debug.WriteLine($"[INFO] Window size changed to {s.Size.Width},{s.Size.Height}");
// Add debounce in scenarios where this event could be hammered.
var idleTime = DateTime.Now - _lastSize;
if (idleTime.TotalSeconds > 0.5d)
{
_lastSize = DateTime.Now;
OnWindowSizeChanged?.Invoke(s.Size);
}
}
if (args.DidPositionChange)
{
// Add debounce in scenarios where this event could be hammered.
var idleTime = DateTime.Now - _lastMove;
if (idleTime.TotalSeconds > 0.5d && LocalConfig != null)
{
_lastMove = DateTime.Now;
if (s.Position.X > 0 && s.Position.Y > 0 && s.Size.Height > 0 && s.Size.Width > 0)
{
// This property is initially null. Once a window has been shown it always has a
// presenter applied, either one applied by the platform or applied by the app itself.
if (s.Presenter is not null && s.Presenter is OverlappedPresenter op)
{
if (op.State == OverlappedPresenterState.Minimized)
{
appWin.IsShownInSwitchers = true; // If accidentally minimized then show the task in the bar.
appWin.TitleBar.IconShowOptions = IconShowOptions.ShowIconAndSystemMenu;
Debug.WriteLine($"[INFO] Window minimized");
}
else if (op.State != OverlappedPresenterState.Maximized)
{
Debug.WriteLine($"[INFO] Updating window position to {s.Position.X},{s.Position.Y} and size to {s.Size.Width},{s.Size.Height}");
LocalConfig.windowX = s.Position.X;
LocalConfig.windowY = s.Position.Y;
LocalConfig.windowH = s.Size.Height;
LocalConfig.windowW = s.Size.Width;
}
else
{
appWin.IsShownInSwitchers = true; // If accidentally maximized then show the task in the bar.
appWin.TitleBar.IconShowOptions = IconShowOptions.ShowIconAndSystemMenu;
Debug.WriteLine($"[INFO] Ignoring position saving (window maximized or restored)");
}
}
}
}
}
};
// Set the application icon.
if (IsPackaged)
appWin.SetIcon(System.IO.Path.Combine(Windows.ApplicationModel.Package.Current.InstalledLocation.Path, $"Assets/StoreLogo.ico"));
else
appWin.SetIcon(System.IO.Path.Combine(AppContext.BaseDirectory, $"Assets/StoreLogo.ico"));
}
m_window.Activate();
// Save the FrameworkElement for any future content dialogs.
MainRoot = m_window.Content as FrameworkElement;
#region [Window placement]
if (LocalConfig == null)
{
Debug.WriteLine($"[INFO] Moving window to center screen");
appWin?.Resize(new Windows.Graphics.SizeInt32(m_width, m_height));
CenterWindow(m_window);
}
else
{
Debug.WriteLine($"[INFO] Moving window to previous position {LocalConfig.windowX},{LocalConfig.windowY} with size {LocalConfig.windowW},{LocalConfig.windowH}");
appWin?.MoveAndResize(new Windows.Graphics.RectInt32(LocalConfig.windowX, LocalConfig.windowY, LocalConfig.windowW, LocalConfig.windowH), Microsoft.UI.Windowing.DisplayArea.Primary);
}
#endregion
}
#region [Window Helpers]
/// <summary>
/// This code example demonstrates how to retrieve an AppWindow from a WinUI3 window.
/// The AppWindow class is available for any top-level HWND in your app.
/// AppWindow is available only to desktop apps (both packaged and unpackaged), it's not available to UWP apps.
/// https://learn.microsoft.com/en-us/windows/apps/windows-app-sdk/windowing/windowing-overview
/// https://learn.microsoft.com/en-us/windows/windows-app-sdk/api/winrt/microsoft.ui.windowing.appwindow.create?view=windows-app-sdk-1.3
/// </summary>
public Microsoft.UI.Windowing.AppWindow? GetAppWindow(object window)
{
// Retrieve the window handle (HWND) of the current (XAML) WinUI3 window.
var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(window);
// For other classes to use (mostly P/Invoke).
App.WindowHandle = hWnd;
// Retrieve the WindowId that corresponds to hWnd.
Microsoft.UI.WindowId windowId = Microsoft.UI.Win32Interop.GetWindowIdFromWindow(hWnd);
// Lastly, retrieve the AppWindow for the current (XAML) WinUI3 window.
Microsoft.UI.Windowing.AppWindow appWindow = Microsoft.UI.Windowing.AppWindow.GetFromWindowId(windowId);
return appWindow;
}
/// <summary>
/// Centers a <see cref="Microsoft.UI.Xaml.Window"/> based on the <see cref="Microsoft.UI.Windowing.DisplayArea"/>.
/// </summary>
/// <remarks>This must be run on the UI thread.</remarks>
public static void CenterWindow(Window window)
{
if (window == null) { return; }
try
{
System.IntPtr hWnd = WinRT.Interop.WindowNative.GetWindowHandle(window);
Microsoft.UI.WindowId windowId = Microsoft.UI.Win32Interop.GetWindowIdFromWindow(hWnd);
if (Microsoft.UI.Windowing.AppWindow.GetFromWindowId(windowId) is Microsoft.UI.Windowing.AppWindow appWindow &&
Microsoft.UI.Windowing.DisplayArea.GetFromWindowId(windowId, Microsoft.UI.Windowing.DisplayAreaFallback.Nearest) is Microsoft.UI.Windowing.DisplayArea displayArea)
{
Windows.Graphics.PointInt32 CenteredPosition = appWindow.Position;
CenteredPosition.X = (displayArea.WorkArea.Width - appWindow.Size.Width) / 2;
CenteredPosition.Y = (displayArea.WorkArea.Height - appWindow.Size.Height) / 2;
appWindow.Move(CenteredPosition);
}
}
catch (Exception ex)
{
Debug.WriteLine($"[ERROR] {MethodBase.GetCurrentMethod()?.Name}: {ex.Message}");
}
}
/// <summary>
/// The <see cref="Microsoft.UI.Windowing.DisplayArea"/> exposes properties such as:
/// OuterBounds (Rect32)
/// WorkArea.Width (int)
/// WorkArea.Height (int)
/// IsPrimary (bool)
/// DisplayId.Value (ulong)
/// </summary>
/// <param name="window"></param>
/// <returns><see cref="DisplayArea"/></returns>
public Microsoft.UI.Windowing.DisplayArea? GetDisplayArea(Window window)
{
try
{
System.IntPtr hWnd = WinRT.Interop.WindowNative.GetWindowHandle(window);
Microsoft.UI.WindowId windowId = Microsoft.UI.Win32Interop.GetWindowIdFromWindow(hWnd);
var da = Microsoft.UI.Windowing.DisplayArea.GetFromWindowId(windowId, Microsoft.UI.Windowing.DisplayAreaFallback.Nearest);
return da;
}
catch (Exception ex)
{
Debug.WriteLine($"[ERROR] {MethodBase.GetCurrentMethod()?.Name}: {ex.Message}");
return null;
}
}
#endregion
#region [Reflection Helpers]
/// <summary>
/// Returns the declaring type's namespace.
/// </summary>
public static string? GetCurrentNamespace() => System.Reflection.MethodBase.GetCurrentMethod()?.DeclaringType?.Namespace;
/// <summary>
/// Returns the declaring type's full name.
/// </summary>
public static string? GetCurrentFullName() => System.Reflection.MethodBase.GetCurrentMethod()?.DeclaringType?.Assembly.FullName;
/// <summary>
/// Returns the declaring type's assembly name.
/// </summary>
public static string? GetCurrentAssemblyName() => System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
/// <summary>
/// Returns the AssemblyVersion, not the FileVersion.
/// </summary>
public static Version GetCurrentAssemblyVersion() => System.Reflection.Assembly.GetExecutingAssembly().GetName().Version ?? new Version();
#endregion
#region [Dialog Helpers]
static SemaphoreSlim semaSlim = new SemaphoreSlim(1, 1);
/// <summary>
/// The <see cref="Windows.UI.Popups.MessageDialog"/> does not look as nice as the
/// <see cref="Microsoft.UI.Xaml.Controls.ContentDialog"/> and is not part of the native Microsoft.UI.Xaml.Controls.
/// The <see cref="Windows.UI.Popups.MessageDialog"/> offers the <see cref="Windows.UI.Popups.UICommandInvokedHandler"/>
/// callback, but this could be replaced with actions. Both can be shown asynchronously.
/// </summary>
/// <remarks>
/// You'll need to call <see cref="WinRT.Interop.InitializeWithWindow.Initialize"/> when using the <see cref="Windows.UI.Popups.MessageDialog"/>,
/// because the <see cref="Microsoft.UI.Xaml.XamlRoot"/> does not exist and an owner must be defined.
/// </remarks>
public static async Task ShowMessageBox(string title, string message, string yesText, string noText, Action? yesAction, Action? noAction)
{
if (App.WindowHandle == IntPtr.Zero) { return; }
// Create the dialog.
var messageDialog = new MessageDialog($"{message}");
messageDialog.Title = title;
if (!string.IsNullOrEmpty(yesText))
{
messageDialog.Commands.Add(new UICommand($"{yesText}", (opt) => { yesAction?.Invoke(); }));
messageDialog.DefaultCommandIndex = 0;
}
if (!string.IsNullOrEmpty(noText))
{
messageDialog.Commands.Add(new UICommand($"{noText}", (opt) => { noAction?.Invoke(); }));
messageDialog.DefaultCommandIndex = 1;
}
// We must initialize the dialog with an owner.
WinRT.Interop.InitializeWithWindow.Initialize(messageDialog, App.WindowHandle);
// Show the message dialog. Our DialogDismissedHandler will deal with what selection the user wants.
await messageDialog.ShowAsync();
// We could force the result in a separate timer...
//DialogDismissedHandler(new UICommand("time-out"));
}
/// <summary>
/// The <see cref="Windows.UI.Popups.MessageDialog"/> does not look as nice as the
/// <see cref="Microsoft.UI.Xaml.Controls.ContentDialog"/> and is not part of the native Microsoft.UI.Xaml.Controls.
/// The <see cref="Windows.UI.Popups.MessageDialog"/> offers the <see cref="Windows.UI.Popups.UICommandInvokedHandler"/>
/// callback, but this could be replaced with actions. Both can be shown asynchronously.
/// </summary>
/// <remarks>
/// You'll need to call <see cref="WinRT.Interop.InitializeWithWindow.Initialize"/> when using the <see cref="Windows.UI.Popups.MessageDialog"/>,
/// because the <see cref="Microsoft.UI.Xaml.XamlRoot"/> does not exist and an owner must be defined.
/// </remarks>
public static async Task ShowMessageBox(string title, string message, string primaryText, string cancelText)
{
// Create the dialog.
var messageDialog = new MessageDialog($"{message}");
messageDialog.Title = title;
if (!string.IsNullOrEmpty(primaryText))
{
messageDialog.Commands.Add(new UICommand($"{primaryText}", new UICommandInvokedHandler(DialogDismissedHandler)));
messageDialog.DefaultCommandIndex = 0;
}
if (!string.IsNullOrEmpty(cancelText))
{
messageDialog.Commands.Add(new UICommand($"{cancelText}", new UICommandInvokedHandler(DialogDismissedHandler)));
messageDialog.DefaultCommandIndex = 1;
}
// We must initialize the dialog with an owner.
WinRT.Interop.InitializeWithWindow.Initialize(messageDialog, App.WindowHandle);
// Show the message dialog. Our DialogDismissedHandler will deal with what selection the user wants.
await messageDialog.ShowAsync();
// We could force the result in a separate timer...
//DialogDismissedHandler(new UICommand("time-out"));
}
/// <summary>
/// Callback for the selected option from the user.
/// </summary>
static void DialogDismissedHandler(IUICommand command)
{
Debug.WriteLine($"[INFO] UICommand.Label ⇨ {command.Label}");
}
/// <summary>
/// The <see cref="Microsoft.UI.Xaml.Controls.ContentDialog"/> looks much better than the
/// <see cref="Windows.UI.Popups.MessageDialog"/> and is part of the native Microsoft.UI.Xaml.Controls.
/// The <see cref="Microsoft.UI.Xaml.Controls.ContentDialog"/> does not offer a <see cref="Windows.UI.Popups.UICommandInvokedHandler"/>
/// callback, but in this example was replaced with actions. Both can be shown asynchronously.
/// </summary>
/// <remarks>
/// There is no need to call <see cref="WinRT.Interop.InitializeWithWindow.Initialize"/> when using the <see cref="Microsoft.UI.Xaml.Controls.ContentDialog"/>,
/// but a <see cref="Microsoft.UI.Xaml.XamlRoot"/> must be defined since it inherits from <see cref="Microsoft.UI.Xaml.Controls.Control"/>.
/// The <see cref="SemaphoreSlim"/> was added to prevent "COMException: Only one ContentDialog can be opened at a time."
/// </remarks>
public static async Task ShowDialogBox(string title, string message, string primaryText, string cancelText, Action? onPrimary, Action? onCancel, Uri? imageUri)
{
if (App.MainRoot?.XamlRoot == null) { return; }
await semaSlim.WaitAsync();
#region [Initialize Assets]
double fontSize = 16;
Microsoft.UI.Xaml.Media.FontFamily fontFamily = new Microsoft.UI.Xaml.Media.FontFamily("Consolas");
if (App.Current.Resources.TryGetValue("FontSizeMedium", out object _))
fontSize = (double)App.Current.Resources["FontSizeMedium"];
if (App.Current.Resources.TryGetValue("PrimaryFont", out object _))
fontFamily = (Microsoft.UI.Xaml.Media.FontFamily)App.Current.Resources["PrimaryFont"];
StackPanel panel = new StackPanel()
{
Orientation = Microsoft.UI.Xaml.Controls.Orientation.Vertical,
Spacing = 10d
};
if (imageUri is not null)
{
panel.Children.Add(new Image
{
Margin = new Thickness(1, -50, 1, 1), // Move the image into the title area.
HorizontalAlignment = Microsoft.UI.Xaml.HorizontalAlignment.Right,
Stretch = Microsoft.UI.Xaml.Media.Stretch.UniformToFill,
Width = 40,
Height = 40,
Source = new BitmapImage(imageUri)
});
}
panel.Children.Add(new TextBlock()
{
Text = message,
FontSize = fontSize,
FontFamily = fontFamily,
HorizontalAlignment = Microsoft.UI.Xaml.HorizontalAlignment.Left,
TextWrapping = Microsoft.UI.Xaml.TextWrapping.Wrap
});
var tb = new TextBox()
{
Text = message,
FontSize = fontSize,
FontFamily = fontFamily,
TextWrapping = TextWrapping.Wrap
};
tb.Loaded += (s, e) => { tb.SelectAll(); };
#endregion
// NOTE: Content dialogs will automatically darken the background.
ContentDialog contentDialog = new ContentDialog()
{
Title = title,
PrimaryButtonText = primaryText,
CloseButtonText = cancelText,
Content = panel,
XamlRoot = App.MainRoot?.XamlRoot,
RequestedTheme = App.MainRoot?.ActualTheme ?? ElementTheme.Default
};
try
{
ContentDialogResult result = await contentDialog.ShowAsync();
switch (result)
{
case ContentDialogResult.Primary:
onPrimary?.Invoke();
break;
//case ContentDialogResult.Secondary:
// onSecondary?.Invoke();
// break;
case ContentDialogResult.None: // Cancel
onCancel?.Invoke();
break;
default:
Debug.WriteLine($"Dialog result not defined.");
break;
}
}
catch (System.Runtime.InteropServices.COMException ex)
{
Debug.WriteLine($"[ERROR] ShowDialogBox: {ex.Message}");
}
finally
{
semaSlim.Release();
}
}
#endregion
#region [Domain Events]
void ApplicationUnhandledException(object sender, Microsoft.UI.Xaml.UnhandledExceptionEventArgs e)
{
// https://docs.microsoft.com/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.application.unhandledexception.
Exception? ex = e.Exception;
Debug.WriteLine($"[UnhandledException]: {ex?.Message}");
Debug.WriteLine($"Unhandled exception of type {ex?.GetType()}: {ex}");
DebugLog($"Unhandled Exception StackTrace: {Environment.StackTrace}");
e.Handled = true;
}
void CurrentDomainOnProcessExit(object? sender, EventArgs e)
{
if (!IsClosing)
IsClosing = true;
if (sender is null)
return;
if (sender is AppDomain ad)
{
Debug.WriteLine($"[OnProcessExit]", $"{nameof(App)}");
Debug.WriteLine($"DomainID: {ad.Id}", $"{nameof(App)}");
Debug.WriteLine($"FriendlyName: {ad.FriendlyName}", $"{nameof(App)}");
Debug.WriteLine($"BaseDirectory: {ad.BaseDirectory}", $"{nameof(App)}");
}
}
void CurrentDomainFirstChanceException(object? sender, System.Runtime.ExceptionServices.FirstChanceExceptionEventArgs e)
{
Debug.WriteLine($"[ERROR] First chance exception from {sender?.GetType()}: {e.Exception.Message}");
DebugLog($"First chance exception from {sender?.GetType()}: {e.Exception.Message}");
if (e.Exception.InnerException != null)
DebugLog($" ⇨ InnerException: {e.Exception.InnerException.Message}");
DebugLog($"First chance exception StackTrace: {Environment.StackTrace}");
}
void CurrentDomainUnhandledException(object? sender, System.UnhandledExceptionEventArgs e)
{
Exception? ex = e.ExceptionObject as Exception;
Debug.WriteLine($"[ERROR] Thread exception of type {ex?.GetType()}: {ex}");
DebugLog($"Thread exception of type {ex?.GetType()}: {ex}");
}
void OnUnobservedTaskException(object? sender, UnobservedTaskExceptionEventArgs e)
{
if (e.Exception is AggregateException aex)
{
aex?.Flatten().Handle(ex =>
{
Debug.WriteLine($"[ERROR] Unobserved task exception: {ex?.Message}");
DebugLog($"Unobserved task exception: {ex?.Message}");
return true;
});
}
e.SetObserved(); // suppress and handle manually
}
#endregion
#region [Miscellaneous]
public static void CloseAllDialogs()
{
if (App.MainRoot?.XamlRoot == null) { return; }
var openedDialogs = VisualTreeHelper.GetOpenPopupsForXamlRoot(App.MainRoot?.XamlRoot);
foreach (var item in openedDialogs)
{
if (item.Child is ContentDialog dialog)
dialog.Hide();
}
}
/// <summary>
/// Simplified debug logger for app-wide use.
/// </summary>
/// <param name="message">the text to append to the file</param>
public static void DebugLog(string message)
{
try
{
if (App.IsPackaged)
System.IO.File.AppendAllText(System.IO.Path.Combine(Windows.ApplicationModel.Package.Current.InstalledLocation.Path, "Debug.log"), $"[{DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss.fff tt")}] {message}{Environment.NewLine}");
else
System.IO.File.AppendAllText(System.IO.Path.Combine(System.AppContext.BaseDirectory, "Debug.log"), $"[{DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss.fff tt")}] {message}{Environment.NewLine}");
}
catch (Exception)
{
Debug.WriteLine($"[{DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss.fff tt")}] {message}");
}
}
/// <summary>
/// Fetching framework version via primitive.
/// </summary>
public string ReflectPrimitive()
{
StringBuilder sb = new StringBuilder();
try
{
Assembly info = typeof(int).Assembly;
var sig = info.Location;
string[] separators = { "\\", "/" };
var list = sig.Split(separators, StringSplitOptions.RemoveEmptyEntries).Skip(4);
sb.Append($"• ");
foreach (var str in list) { sb.Append($"{str} • "); }
}
catch (Exception) { }
return $"{sb}";
}
/// <summary>
/// Returns an exhaustive list of all modules involved in the current process.
/// </summary>
public static List<string> GetProcessDependencies()
{
List<string> result = new List<string>();
try
{
string self = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name + ".exe";
System.Diagnostics.ProcessModuleCollection pmc = System.Diagnostics.Process.GetCurrentProcess().Modules;
IOrderedEnumerable<System.Diagnostics.ProcessModule> pmQuery = pmc
.OfType<System.Diagnostics.ProcessModule>()
.Where(pt => pt.ModuleMemorySize > 0)
.OrderBy(o => o.ModuleName);
foreach (var item in pmQuery)
{
//if (!item.ModuleName.Contains($"{self}"))
result.Add($"Module name: {item.ModuleName}, {(string.IsNullOrEmpty(item.FileVersionInfo.FileVersion) ? "version unknown" : $"v{item.FileVersionInfo.FileVersion}")}");
try { item.Dispose(); }
catch { }
}
}
catch (Exception ex)
{
Debug.WriteLine($"[ERROR] ProcessModuleCollection: {ex.Message}");
}
return result;
}
/// <summary>
/// Returns the basic assemblies needed by the application.
/// </summary>
public static Dictionary<string, Version> GetReferencedAssemblies(bool addSelf = false)
{
Dictionary<string, Version> values = new Dictionary<string, Version>();
try
{
var assem = Assembly.GetExecutingAssembly();
int idx = 0; // to prevent key collisions only
if (addSelf)
values.Add($"{(++idx).AddOrdinal()}: {assem.GetName().Name}", assem.GetName().Version ?? new Version()); // add self
IOrderedEnumerable<AssemblyName> names = assem.GetReferencedAssemblies().OrderBy(o => o.Name);
foreach (var sas in names)
{
values.Add($"{(++idx).AddOrdinal()}: {sas.Name}", sas.Version ?? new Version());
}
}
catch (Exception ex)
{
Debug.WriteLine($"[ERROR] GetReferencedAssemblies: {ex.Message}");
}
return values;
}
#endregion
}