繁体   English   中英

如何获取 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));
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM