首页 > 解决方案 > 如何获取 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课堂上的通知内容。

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

标签: c#uwpnotificationswindows-10

解决方案


找到了!(总是在我提出问题后发生)。为了后代的缘故,这是答案:

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

推荐阅读