mirror of
https://github.com/PacktPublishing/Learn-WinUI-3-Second-Edition.git
synced 2026-06-20 12:23:09 +00:00
131 lines
4.7 KiB
C#
131 lines
4.7 KiB
C#
using Microsoft.Extensions.DependencyInjection;
|
|
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.Runtime.InteropServices;
|
|
using System.Threading.Tasks;
|
|
using WinRT.Interop;
|
|
|
|
namespace MyMediaCollection
|
|
{
|
|
/// <summary>
|
|
/// Provides application-specific behavior to supplement the default Application class.
|
|
/// </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>
|
|
/// 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();
|
|
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(LaunchActivatedEventArgs args)
|
|
{
|
|
m_window = new MainWindow();
|
|
var rootFrame = new Frame();
|
|
await RegisterComponentsAsync(rootFrame);
|
|
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();
|
|
}
|
|
|
|
private void RootFrame_NavigationFailed(object sender, NavigationFailedEventArgs e)
|
|
{
|
|
throw new Exception($"Error loading page {e.SourcePageType.FullName}");
|
|
}
|
|
|
|
private static Window m_window;
|
|
|
|
internal Window Window => m_window;
|
|
|
|
private async Task RegisterComponentsAsync(Frame rootFrame)
|
|
{
|
|
var navigationService = new NavigationService(rootFrame);
|
|
navigationService.Configure(nameof(MainPage), typeof(MainPage));
|
|
navigationService.Configure(nameof(ItemDetailsPage), typeof(ItemDetailsPage));
|
|
var dataService = new SqliteDataService();
|
|
await dataService.InitializeDataAsync();
|
|
|
|
HostContainer = Host.CreateDefaultBuilder()
|
|
.ConfigureServices(services =>
|
|
{
|
|
services.AddSingleton<INavigationService>(navigationService);
|
|
services.AddSingleton<IDataService>(dataService);
|
|
services.AddTransient<MainViewModel>();
|
|
services.AddTransient<ItemDetailsViewModel>();
|
|
}).Build();
|
|
}
|
|
}
|
|
} |