mirror of
https://github.com/PacktPublishing/Learn-WinUI-3-Second-Edition.git
synced 2026-06-20 12:23:09 +00:00
Update completed solution for chapter 8
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
using Microsoft.Windows.AppNotifications;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace MyMediaCollection.Helpers
|
||||
{
|
||||
internal class NotificationManager
|
||||
{
|
||||
private bool isRegistered;
|
||||
private Dictionary<int, Action<AppNotificationActivatedEventArgs>> notificationHandlers;
|
||||
|
||||
public NotificationManager()
|
||||
{
|
||||
isRegistered = false;
|
||||
|
||||
// When adding new a scenario, be sure to add its notification handler here.
|
||||
notificationHandlers = new Dictionary<int, Action<AppNotificationActivatedEventArgs>>
|
||||
{
|
||||
{ ToastWithAvatar.ScenarioId, ToastWithAvatar.NotificationReceived },
|
||||
{ ToastWithText.ScenarioId, ToastWithText.NotificationReceived }
|
||||
};
|
||||
}
|
||||
|
||||
~NotificationManager()
|
||||
{
|
||||
Unregister();
|
||||
}
|
||||
|
||||
public void Unregister()
|
||||
{
|
||||
if (isRegistered)
|
||||
{
|
||||
AppNotificationManager.Default.Unregister();
|
||||
isRegistered = false;
|
||||
}
|
||||
}
|
||||
|
||||
public void Init()
|
||||
{
|
||||
AppNotificationManager notificationManager = AppNotificationManager.Default;
|
||||
|
||||
// Add handler before calling Register.
|
||||
notificationManager.NotificationInvoked += OnNotificationInvoked;
|
||||
notificationManager.Register();
|
||||
|
||||
isRegistered = true;
|
||||
}
|
||||
|
||||
public void ProcessLaunchActivationArgs(AppNotificationActivatedEventArgs notificationActivatedEventArgs)
|
||||
{
|
||||
DispatchNotification(notificationActivatedEventArgs);
|
||||
NotificationShared.AppLaunchedFromNotification();
|
||||
}
|
||||
|
||||
private bool DispatchNotification(AppNotificationActivatedEventArgs notificationActivatedEventArgs)
|
||||
{
|
||||
var scenarioId = notificationActivatedEventArgs.Arguments[NotificationShared.scenarioTag];
|
||||
if (scenarioId.Length != 0)
|
||||
{
|
||||
try
|
||||
{
|
||||
notificationHandlers[int.Parse(scenarioId)](notificationActivatedEventArgs);
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
// No matching NotificationHandler for scenarioId.
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// No scenarioId provided
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public void OnNotificationInvoked(object sender, AppNotificationActivatedEventArgs notificationActivatedEventArgs)
|
||||
{
|
||||
NotificationShared.NotificationReceived();
|
||||
|
||||
if (!DispatchNotification(notificationActivatedEventArgs))
|
||||
{
|
||||
NotificationShared.UnrecognizedToastOriginator();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
using Microsoft.UI.Xaml.Controls;
|
||||
using MyMediaCollection.Views;
|
||||
|
||||
namespace MyMediaCollection.Helpers
|
||||
{
|
||||
public class NotificationShared
|
||||
{
|
||||
public const string scenarioTag = "scenarioId";
|
||||
|
||||
public struct Notification
|
||||
{
|
||||
public string Originator;
|
||||
public string Action;
|
||||
public bool HasInput;
|
||||
public string Input;
|
||||
};
|
||||
|
||||
public static void CouldNotSendToast()
|
||||
{
|
||||
MainPage.Current.NotifyUser("Could not send toast", InfoBarSeverity.Error);
|
||||
}
|
||||
|
||||
public static void ToastSentSuccessfully()
|
||||
{
|
||||
MainPage.Current.NotifyUser("Toast sent successfully!", InfoBarSeverity.Success);
|
||||
}
|
||||
|
||||
public static void AppLaunchedFromNotification()
|
||||
{
|
||||
MainPage.Current.NotifyUser("App launched from notifications", InfoBarSeverity.Informational);
|
||||
}
|
||||
|
||||
public static void NotificationReceived()
|
||||
{
|
||||
MainPage.Current.NotifyUser("Notification received", InfoBarSeverity.Informational);
|
||||
}
|
||||
|
||||
public static void UnrecognizedToastOriginator()
|
||||
{
|
||||
MainPage.Current.NotifyUser("Unrecognized Toast Originator or Unknown Error", InfoBarSeverity.Error);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
using Microsoft.Windows.AppNotifications.Builder;
|
||||
using Microsoft.Windows.AppNotifications;
|
||||
using MyMediaCollection.Views;
|
||||
|
||||
namespace MyMediaCollection.Helpers
|
||||
{
|
||||
public class ToastWithAvatar
|
||||
{
|
||||
public const int ScenarioId = 1;
|
||||
public const string ScenarioName = "Local Toast with Image";
|
||||
|
||||
public static bool SendToast()
|
||||
{
|
||||
var appNotification = new AppNotificationBuilder()
|
||||
.AddArgument("action", "ToastClick")
|
||||
.AddArgument(NotificationShared.scenarioTag, ScenarioId.ToString())
|
||||
.SetAppLogoOverride(new System.Uri($"file://{App.GetFullPathToAsset("Square150x150Logo.scale-200.png")}"), AppNotificationImageCrop.Circle)
|
||||
.AddText(ScenarioName)
|
||||
.AddText("This is a notification message.")
|
||||
.AddButton(new AppNotificationButton("Open App")
|
||||
.AddArgument("action", "OpenApp")
|
||||
.AddArgument(NotificationShared.scenarioTag, ScenarioId.ToString()))
|
||||
.BuildNotification();
|
||||
|
||||
AppNotificationManager.Default.Show(appNotification);
|
||||
|
||||
// If notification is sent, it will have an Id. Success.
|
||||
return appNotification.Id != 0;
|
||||
}
|
||||
|
||||
public static void NotificationReceived(AppNotificationActivatedEventArgs notificationActivatedEventArgs)
|
||||
{
|
||||
var notification = new NotificationShared.Notification
|
||||
{
|
||||
Originator = ScenarioName,
|
||||
Action = notificationActivatedEventArgs.Arguments["action"]
|
||||
};
|
||||
MainPage.Current.NotificationReceived(notification);
|
||||
App.ToForeground();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
using Microsoft.Windows.AppNotifications.Builder;
|
||||
using Microsoft.Windows.AppNotifications;
|
||||
using MyMediaCollection.Views;
|
||||
|
||||
namespace MyMediaCollection.Helpers
|
||||
{
|
||||
public class ToastWithText
|
||||
{
|
||||
public const int ScenarioId = 2;
|
||||
public const string ScenarioName = "Local Toast with Image and Text Entry";
|
||||
const string textboxReplyId = "textboxReply";
|
||||
|
||||
public static bool SendToast()
|
||||
{
|
||||
var appNotification = new AppNotificationBuilder()
|
||||
.AddArgument("action", "ToastClick")
|
||||
.AddArgument(NotificationShared.scenarioTag, ScenarioId.ToString())
|
||||
.SetAppLogoOverride(new System.Uri($"file://{App.GetFullPathToAsset("Square150x150Logo.scale-200.png")}"), AppNotificationImageCrop.Circle)
|
||||
.AddText(ScenarioName)
|
||||
.AddText("This is a notification message.")
|
||||
.AddTextBox(textboxReplyId, "Enter a reply", "Reply box")
|
||||
.AddButton(new AppNotificationButton("Reply")
|
||||
.AddArgument("action", "Reply")
|
||||
.AddArgument(NotificationShared.scenarioTag, ScenarioId.ToString())
|
||||
.SetInputId(textboxReplyId))
|
||||
.BuildNotification();
|
||||
|
||||
AppNotificationManager.Default.Show(appNotification);
|
||||
|
||||
// If notification is sent, it will have an Id. Success.
|
||||
return appNotification.Id != 0;
|
||||
}
|
||||
|
||||
public static void NotificationReceived(AppNotificationActivatedEventArgs notificationActivatedEventArgs)
|
||||
{
|
||||
var notification = new NotificationShared.Notification
|
||||
{
|
||||
Originator = ScenarioName,
|
||||
Action = notificationActivatedEventArgs.Arguments["action"],
|
||||
HasInput = true,
|
||||
Input = notificationActivatedEventArgs.UserInput[textboxReplyId]
|
||||
};
|
||||
MainPage.Current.NotificationReceived(notification);
|
||||
App.ToForeground();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user