Update completed solution for chapter 8

This commit is contained in:
Alvin Ashcraft
2023-07-29 14:58:47 -04:00
parent 21e22a0369
commit 4c9e111e61
10 changed files with 387 additions and 7 deletions
@@ -3,13 +3,17 @@ using Microsoft.Extensions.Hosting;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Navigation;
using Microsoft.Windows.AppLifecycle;
using Microsoft.Windows.AppNotifications;
using MyMediaCollection.Helpers;
using MyMediaCollection.Interfaces;
using MyMediaCollection.Services;
using MyMediaCollection.ViewModels;
using MyMediaCollection.Views;
using System;
using System.Net;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using WinRT.Interop;
namespace MyMediaCollection
{
@@ -18,6 +22,11 @@ namespace MyMediaCollection
/// </summary>
public partial class App : Application
{
[DllImport("user32.dll", SetLastError = true)]
static extern void SwitchToThisWindow(IntPtr hWnd, bool turnOn);
private NotificationManager notificationManager;
public static IHost HostContainer { get; private set; }
/// <summary>
@@ -27,13 +36,45 @@ namespace MyMediaCollection
public App()
{
this.InitializeComponent();
notificationManager = new NotificationManager();
notificationManager.Init();
AppDomain.CurrentDomain.ProcessExit += CurrentDomain_ProcessExit;
}
private void CurrentDomain_ProcessExit(object sender, EventArgs e)
{
notificationManager.Unregister();
}
public static void ToForeground()
{
if (m_window != null)
{
IntPtr handle = WindowNative.GetWindowHandle(m_window);
if (handle != IntPtr.Zero)
{
SwitchToThisWindow(handle, true);
}
}
}
public static string GetFullPathToExe()
{
var path = AppDomain.CurrentDomain.BaseDirectory;
var pos = path.LastIndexOf("\\");
return path.Substring(0, pos);
}
public static string GetFullPathToAsset(string assetName)
{
return $"{GetFullPathToExe()}\\Assets\\{assetName}";
}
/// <summary>
/// Invoked when the application is launched.
/// </summary>
/// <param name="args">Details about the launch request and process.</param>
protected override async void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
protected override async void OnLaunched(LaunchActivatedEventArgs args)
{
m_window = new MainWindow();
var rootFrame = new Frame();
@@ -41,6 +82,22 @@ namespace MyMediaCollection
rootFrame.NavigationFailed += RootFrame_NavigationFailed;
rootFrame.Navigate(typeof(MainPage), args);
m_window.Content = rootFrame;
var currentInstance = AppInstance.GetCurrent();
if (currentInstance.IsCurrent)
{
AppActivationArguments activationArgs = currentInstance.GetActivatedEventArgs();
if (activationArgs != null)
{
ExtendedActivationKind extendedKind = activationArgs.Kind;
if (extendedKind == ExtendedActivationKind.AppNotification)
{
var notificationActivatedEventArgs = (AppNotificationActivatedEventArgs)activationArgs.Data;
notificationManager.ProcessLaunchActivationArgs(notificationActivatedEventArgs);
}
}
}
m_window.Activate();
}
@@ -49,7 +106,7 @@ namespace MyMediaCollection
throw new Exception($"Error loading page {e.SourcePageType.FullName}");
}
private Window m_window;
private static Window m_window;
internal Window Window => m_window;