如何获取 Windows 10 的内容?UWP/C# 中的通知?
How can I get the content of a Windows 10 ?Notification in a UWP/C#?
我正在尝试获取用户通知中的文本,以及单击通知时发生的操作。我得到用户的许可来阅读它们(使用 UserNotificationListener.RequestAccessAsync()
),然后我遍历它们,并将它们添加到 ListView:
private async void getNotificationsAsync()
{
UserNotificationListener listener = UserNotificationListener.Current;
IReadOnlyList<UserNotification> notifs = await listener.GetNotificationsAsync(NotificationKinds.Toast);
NotificationsList.Items.Clear();
foreach (UserNotification notif in notifs)
{
//Console.WriteLine(notif.AppInfo.DisplayInfo.DisplayName);
NotificationsList.Items.Add(notif.AppInfo.DisplayInfo.DisplayName);
}
}
但我能从 UserNotification class 得到的只是启动应用程序的名称(以及通知发生的时间)。我找不到任何方法来访问 UserNotification
class 中的通知内容。
我正在尝试的可能吗?我用的是右class吗?
找到了! (总是在 我问这个问题之后发生)。为了后代,这里是答案:
NotificationBinding toastBinding = notif.Notification.Visual.GetBinding(KnownNotificationBindings.ToastGeneric);
if (toastBinding != null)
{
// And then get the text elements from the toast binding
IReadOnlyList<AdaptiveNotificationText> textElements = toastBinding.GetTextElements();
// Treat the first text element as the title text
string titleText = textElements.FirstOrDefault()?.Text;
// We'll treat all subsequent text elements as body text,
// joining them together via newlines.
string bodyText = string.Join("\n", textElements.Skip(1).Select(t => t.Text));
}
我正在尝试获取用户通知中的文本,以及单击通知时发生的操作。我得到用户的许可来阅读它们(使用 UserNotificationListener.RequestAccessAsync()
),然后我遍历它们,并将它们添加到 ListView:
private async void getNotificationsAsync()
{
UserNotificationListener listener = UserNotificationListener.Current;
IReadOnlyList<UserNotification> notifs = await listener.GetNotificationsAsync(NotificationKinds.Toast);
NotificationsList.Items.Clear();
foreach (UserNotification notif in notifs)
{
//Console.WriteLine(notif.AppInfo.DisplayInfo.DisplayName);
NotificationsList.Items.Add(notif.AppInfo.DisplayInfo.DisplayName);
}
}
但我能从 UserNotification class 得到的只是启动应用程序的名称(以及通知发生的时间)。我找不到任何方法来访问 UserNotification
class 中的通知内容。
我正在尝试的可能吗?我用的是右class吗?
找到了! (总是在 我问这个问题之后发生)。为了后代,这里是答案:
NotificationBinding toastBinding = notif.Notification.Visual.GetBinding(KnownNotificationBindings.ToastGeneric);
if (toastBinding != null)
{
// And then get the text elements from the toast binding
IReadOnlyList<AdaptiveNotificationText> textElements = toastBinding.GetTextElements();
// Treat the first text element as the title text
string titleText = textElements.FirstOrDefault()?.Text;
// We'll treat all subsequent text elements as body text,
// joining them together via newlines.
string bodyText = string.Join("\n", textElements.Skip(1).Select(t => t.Text));
}