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,17 @@
|
||||
namespace TemplateStudioSampleApp.Activation;
|
||||
|
||||
// Extend this class to implement new ActivationHandlers. See DefaultActivationHandler for an example.
|
||||
// https://github.com/microsoft/TemplateStudio/blob/main/docs/WinUI/activation.md
|
||||
public abstract class ActivationHandler<T> : IActivationHandler
|
||||
where T : class
|
||||
{
|
||||
// Override this method to add the logic for whether to handle the activation.
|
||||
protected virtual bool CanHandleInternal(T args) => true;
|
||||
|
||||
// Override this method to add the logic for your activation handler.
|
||||
protected abstract Task HandleInternalAsync(T args);
|
||||
|
||||
public bool CanHandle(object args) => args is T && CanHandleInternal((args as T)!);
|
||||
|
||||
public async Task HandleAsync(object args) => await HandleInternalAsync((args as T)!);
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
using Microsoft.UI.Xaml;
|
||||
|
||||
using TemplateStudioSampleApp.Contracts.Services;
|
||||
using TemplateStudioSampleApp.ViewModels;
|
||||
|
||||
namespace TemplateStudioSampleApp.Activation;
|
||||
|
||||
public class DefaultActivationHandler : ActivationHandler<LaunchActivatedEventArgs>
|
||||
{
|
||||
private readonly INavigationService _navigationService;
|
||||
|
||||
public DefaultActivationHandler(INavigationService navigationService)
|
||||
{
|
||||
_navigationService = navigationService;
|
||||
}
|
||||
|
||||
protected override bool CanHandleInternal(LaunchActivatedEventArgs args)
|
||||
{
|
||||
// None of the ActivationHandlers has handled the activation.
|
||||
return _navigationService.Frame?.Content == null;
|
||||
}
|
||||
|
||||
protected async override Task HandleInternalAsync(LaunchActivatedEventArgs args)
|
||||
{
|
||||
_navigationService.NavigateTo(typeof(MainViewModel).FullName!, args.Arguments);
|
||||
|
||||
await Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace TemplateStudioSampleApp.Activation;
|
||||
|
||||
public interface IActivationHandler
|
||||
{
|
||||
bool CanHandle(object args);
|
||||
|
||||
Task HandleAsync(object args);
|
||||
}
|
||||
Reference in New Issue
Block a user