【问题标题】:Access to Windows notification from WPF从 WPF 访问 Windows 通知
【发布时间】:2021-03-16 15:03:06
【问题描述】:

如何从 WPF 获得对 Windows 通知的访问权限?

我试过这种方式。我得到了accessStatus 允许。

但我得到了例外 System.Exception: 'Element not found. (Exception from HRESULT: 0x80070490)

这是我的代码:

using Windows.UI.Notifications.Management;
***
public partial class NotificationWindow : Window
    {
        UserNotificationListener listener;
        public NotificationWindow()
        {
            InitializeComponent();
            SubscribeToNotif();
        }

        public async void SubscribeToNotif()
        {    
            listener = UserNotificationListener.Current;
            
            UserNotificationListenerAccessStatus accessStatus = await listener.RequestAccessAsync();

            if(accessStatus == UserNotificationListenerAccessStatus.Allowed)
                listener.NotificationChanged += Listener_NotificationChanged;
        }

        private void Listener_NotificationChanged(UserNotificationListener sender, Windows.UI.Notifications.UserNotificationChangedEventArgs args)
        {
            Console.WriteLine("Got notif");
        }
    }

【问题讨论】:

    标签: c# wpf windows notifications


    【解决方案1】:

    解决方案: 将WPF项目添加到Windows Application Packaging Project

    【讨论】:

    • 遇到同样的问题,使用 net5.0-windows10.0.17763.0 将 WPF 添加到 WAPP,并没有为我解决:(。
    • 我解决了我的问题,在我的情况下,它是我的 windows 机器,它必须进行一些修改,以便对侦听器的任何调用都会导致“找不到元素”,在另一台机器上测试异常消失了在那里。
    猜你喜欢
    • 2016-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多