mirror of
https://github.com/PacktPublishing/Learn-WinUI-3-Second-Edition.git
synced 2026-06-20 12:23:09 +00:00
Add chapter 10 sample project
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
using Microsoft.UI.Xaml;
|
||||
using Microsoft.UI.Xaml.Controls;
|
||||
|
||||
using TemplateStudioSampleApp.Activation;
|
||||
using TemplateStudioSampleApp.Contracts.Services;
|
||||
using TemplateStudioSampleApp.Views;
|
||||
|
||||
namespace TemplateStudioSampleApp.Services;
|
||||
|
||||
public class ActivationService : IActivationService
|
||||
{
|
||||
private readonly ActivationHandler<LaunchActivatedEventArgs> _defaultHandler;
|
||||
private readonly IEnumerable<IActivationHandler> _activationHandlers;
|
||||
private readonly IThemeSelectorService _themeSelectorService;
|
||||
private UIElement? _shell = null;
|
||||
|
||||
public ActivationService(ActivationHandler<LaunchActivatedEventArgs> defaultHandler, IEnumerable<IActivationHandler> activationHandlers, IThemeSelectorService themeSelectorService)
|
||||
{
|
||||
_defaultHandler = defaultHandler;
|
||||
_activationHandlers = activationHandlers;
|
||||
_themeSelectorService = themeSelectorService;
|
||||
}
|
||||
|
||||
public async Task ActivateAsync(object activationArgs)
|
||||
{
|
||||
// Execute tasks before activation.
|
||||
await InitializeAsync();
|
||||
|
||||
// Set the MainWindow Content.
|
||||
if (App.MainWindow.Content == null)
|
||||
{
|
||||
_shell = App.GetService<ShellPage>();
|
||||
App.MainWindow.Content = _shell ?? new Frame();
|
||||
}
|
||||
|
||||
// Handle activation via ActivationHandlers.
|
||||
await HandleActivationAsync(activationArgs);
|
||||
|
||||
// Activate the MainWindow.
|
||||
App.MainWindow.Activate();
|
||||
|
||||
// Execute tasks after activation.
|
||||
await StartupAsync();
|
||||
}
|
||||
|
||||
private async Task HandleActivationAsync(object activationArgs)
|
||||
{
|
||||
var activationHandler = _activationHandlers.FirstOrDefault(h => h.CanHandle(activationArgs));
|
||||
|
||||
if (activationHandler != null)
|
||||
{
|
||||
await activationHandler.HandleAsync(activationArgs);
|
||||
}
|
||||
|
||||
if (_defaultHandler.CanHandle(activationArgs))
|
||||
{
|
||||
await _defaultHandler.HandleAsync(activationArgs);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task InitializeAsync()
|
||||
{
|
||||
await _themeSelectorService.InitializeAsync().ConfigureAwait(false);
|
||||
await Task.CompletedTask;
|
||||
}
|
||||
|
||||
private async Task StartupAsync()
|
||||
{
|
||||
await _themeSelectorService.SetRequestedThemeAsync();
|
||||
await Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user