Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up App.xaml.cs #1628

Merged
merged 2 commits into from
Jan 12, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 42 additions & 99 deletions WinUIGallery/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@
using Windows.ApplicationModel.Activation;
using WinUIGallery.DesktopWap.DataModel;
using WASDK = Microsoft.WindowsAppSDK;
using System.Text;
using Windows.System;
using System.Runtime.InteropServices;
using Microsoft.Windows.AppNotifications;
using Microsoft.Windows.AppNotifications.Builder;
using System.Collections.Generic;
using static WinUIGallery.Win32;

namespace WinUIGallery
Expand Down Expand Up @@ -55,7 +53,7 @@ public static string WinAppSdkRuntimeDetails
try
{
// Retrieve Windows App Runtime version info dynamically
var windowsAppRuntimeVersion =
IEnumerable<FileVersionInfo> windowsAppRuntimeVersion =
from module in Process.GetCurrentProcess().Modules.OfType<ProcessModule>()
where module.FileName.EndsWith("Microsoft.WindowsAppRuntime.Insights.Resource.dll")
select FileVersionInfo.GetVersionInfo(module.FileName);
Expand All @@ -68,43 +66,31 @@ where module.FileName.EndsWith("Microsoft.WindowsAppRuntime.Insights.Resource.dl
}
}

// Get the initial window created for this app
// On UWP, this is simply Window.Current
// On Desktop, multiple Windows may be created, and the StartupWindow may have already
// been closed.
/// <summary>
/// Get the initial window created for this app.
/// </summary>
public static Window StartupWindow
{
get
{
return startupWindow;
}
get => startupWindow;
}

/// <summary>
/// Initializes the singleton Application object. This is the first line of authored code
/// 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()
{
this.InitializeComponent();
this.UnhandledException += HandleExceptions;

#if WINUI_PRERELEASE
niels9001 marked this conversation as resolved.
Show resolved Hide resolved
this.Suspending += OnSuspending;
this.Resuming += App_Resuming;
this.RequiresPointerMode = ApplicationRequiresPointerMode.WhenRequested;
#endif
}

public void EnableSound(bool withSpatial = false)
{
ElementSoundPlayer.State = ElementSoundPlayerState.On;
niels9001 marked this conversation as resolved.
Show resolved Hide resolved

if (!withSpatial)
ElementSoundPlayer.SpatialAudioMode = ElementSpatialAudioMode.Off;
else
ElementSoundPlayer.SpatialAudioMode = ElementSpatialAudioMode.On;
InitializeComponent();
UnhandledException += HandleExceptions;
}

/// <summary>
/// Converts a string into a enum.
/// </summary>
/// <typeparam name="TEnum">The output enum type.</typeparam>
/// <param name="text">The input text.</param>
/// <returns>The parsed enum.</returns>
/// <exception cref="InvalidOperationException">Thrown when the TEnum type is not a enum.</exception>
public static TEnum GetEnum<TEnum>(string text) where TEnum : struct
{
if (!typeof(TEnum).GetTypeInfo().IsEnum)
Expand All @@ -130,13 +116,13 @@ protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs ar
win32WindowHelper.SetWindowMinMaxSize(new Win32WindowHelper.POINT() { x = 500, y = 500 });

#if DEBUG
if (System.Diagnostics.Debugger.IsAttached)
if (Debugger.IsAttached)
{
this.DebugSettings.BindingFailed += DebugSettings_BindingFailed;
DebugSettings.BindingFailed += DebugSettings_BindingFailed;
}
#endif

keyEventHook = new HookProc(KeyEventHook);
keyEventHook = KeyEventHook;
registeredKeyPressedHook = SetWindowKeyHook(keyEventHook);

EnsureWindow();
Expand All @@ -154,58 +140,27 @@ private int KeyEventHook(int nCode, IntPtr wParam, IntPtr lParam)

private void DebugSettings_BindingFailed(object sender, BindingFailedEventArgs e)
{

}

#if WINUI_PRERELEASE
protected override void OnActivated(IActivatedEventArgs args)
{
EnsureWindow(args);
throw new Exception($"A debug binding failed: " + e.Message);
}
#endif

private async void EnsureWindow(IActivatedEventArgs args = null)
private async void EnsureWindow()
{
// No matter what our destination is, we're going to need control data loaded - let's knock that out now.
// We'll never need to do this again.
await ControlInfoDataSource.Instance.GetGroupsAsync();
await IconsDataSource.Instance.LoadIcons();

Frame rootFrame = GetRootFrame();

ThemeHelper.Initialize();

Type targetPageType = typeof(HomePage);
string targetPageArguments = string.Empty;
var targetPageType = typeof(HomePage);
var targetPageArguments = string.Empty;

if (args != null)
AppActivationArguments eventArgs = AppInstance.GetCurrent().GetActivatedEventArgs();
if (eventArgs != null && eventArgs.Kind == ExtendedActivationKind.Protocol && eventArgs.Data is ProtocolActivatedEventArgs)
{
if (args.Kind == ActivationKind.Launch)
{
if (args.PreviousExecutionState == ApplicationExecutionState.Terminated)
{
try
{
await SuspensionManager.RestoreAsync();
}
catch (SuspensionManagerException)
{
//Something went wrong restoring state.
//Assume there is no state and continue
}
}

targetPageArguments = ((Windows.ApplicationModel.Activation.LaunchActivatedEventArgs)args).Arguments;
}
}
var eventargs = Microsoft.Windows.AppLifecycle.AppInstance.GetCurrent().GetActivatedEventArgs();
if (eventargs != null && eventargs.Kind is ExtendedActivationKind.Protocol && eventargs.Data is ProtocolActivatedEventArgs)
{
ProtocolActivatedEventArgs ProtocolArgs = eventargs.Data as ProtocolActivatedEventArgs;
var uri = ProtocolArgs.Uri.LocalPath.Replace("/", "");

var ProtocolArgs = eventArgs.Data as ProtocolActivatedEventArgs;
string uri = ProtocolArgs.Uri.LocalPath.Replace("/", "");
targetPageArguments = uri;
string targetId = string.Empty;

if (uri == "AllControls")
{
Expand All @@ -225,23 +180,32 @@ private async void EnsureWindow(IActivatedEventArgs args = null)
}
}

NavigationRootPage rootPage = StartupWindow.Content as NavigationRootPage;
var rootPage = StartupWindow.Content as NavigationRootPage;
rootPage.Navigate(targetPageType, targetPageArguments);

if (targetPageType == typeof(HomePage))
{
((Microsoft.UI.Xaml.Controls.NavigationViewItem)((NavigationRootPage)App.StartupWindow.Content).NavigationView.MenuItems[0]).IsSelected = true;
var navItem = (NavigationViewItem) rootPage.NavigationView.MenuItems[0];
navItem.IsSelected = true;
}

// Ensure the current window is active
// Activate the startup window.
StartupWindow.Activate();
}

/// <summary>
/// Gets the frame of the StartupWindow.
/// </summary>
/// <returns>The frame of the StartupWindow.</returns>
/// <exception cref="Exception">Thrown if the window doesn't have a frame with the name "rootFrame".</exception>
public Frame GetRootFrame()
{
Frame rootFrame;
NavigationRootPage rootPage = StartupWindow.Content as NavigationRootPage;
if (rootPage == null)
if (StartupWindow.Content is NavigationRootPage rootPage)
{
rootFrame = (Frame)rootPage.FindName("rootFrame");
}
else
{
rootPage = new NavigationRootPage();
rootFrame = (Frame)rootPage.FindName("rootFrame");
Expand All @@ -255,10 +219,6 @@ public Frame GetRootFrame()

StartupWindow.Content = rootPage;
}
else
{
rootFrame = (Frame)rootPage.FindName("rootFrame");
}

return rootFrame;
}
Expand Down Expand Up @@ -293,22 +253,5 @@ private void HandleExceptions(object sender, Microsoft.UI.Xaml.UnhandledExceptio
//Show the notification
AppNotificationManager.Default.Show(notification);
}


#if WINUI_PRERELEASE
/// <summary>
/// Invoked when application execution is being suspended. Application state is saved
/// without knowing whether the application will be terminated or resumed with the contents
/// of memory still intact.
/// </summary>
/// <param name="sender">The source of the suspend request.</param>
/// <param name="e">Details about the suspend request.</param>
private async void OnSuspending(object sender, SuspendingEventArgs e)
{
var deferral = e.SuspendingOperation.GetDeferral();
await SuspensionManager.SaveAsync();
deferral.Complete();
}
#endif // WINUI_PRERELEASE
}
}