如何获取 Windows 10 的内容?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 类中得到的只是启动应用程序的名称(以及通知发生的时间(。我找不到任何方法来访问UserNotification类中的通知内容。

我正在尝试的可能吗?我是否使用了正确的课程?

找到了!(总是我问问题😀之后发生(。为了后人着想,答案如下:

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));
}

最新更新