如何以UWP/C#格式获取Windows 10?通知的内容? [英] How can I get the content of a Windows 10 ?Notification in a UWP/C#?

查看:62
本文介绍了如何以UWP/C#格式获取Windows 10?通知的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取用户通知中的文本,以及在您单击通知时发生的操作。我获得了用户读取它们的权限(使用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类中通知内容的任何方法。

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

推荐答案

找到了!(总是发生在我问问题😀之后)。为了子孙后代,以下是答案:

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("
", textElements.Skip(1).Select(t => t.Text));
}

这篇关于如何以UWP/C#格式获取Windows 10?通知的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆