You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

注册UserNotificationListener事件触发ElementNotFound异常问题排查

问题原因及解决方法

你遇到的0x80070490异常并不是事件注册方式的问题,而是没有正确处理UserNotificationListener的权限验证和系统兼容性检查。这个API属于UWP范畴,在WinForms中使用必须满足几个前提条件:

核心问题点

  1. 该API仅支持Windows 10 1709(秋季创意者更新)及以上版本,低于此版本系统直接调用会报错。
  2. 必须先向用户请求访问权限,未授权状态下调用UserNotificationListener.Current会触发COM异常。

修正后的代码示例

Imports Windows.UI.Notifications
Imports Windows.UI.Notifications.Management

Public Class Form1
    Private WithEvents listen As UserNotificationListener

    Private Async Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' 第一步:检查API是否支持当前系统版本
        If Not UserNotificationListener.IsSupported() Then
            MsgBox("当前系统版本不支持UserNotificationListener API")
            Return
        End If

        ' 第二步:请求用户授权
        Dim accessStatus = Await UserNotificationListener.RequestAccessAsync()
        Select Case accessStatus
            Case UserNotificationListenerAccessStatus.Allowed
                ' 授权成功后再初始化Listener并注册事件
                listen = UserNotificationListener.Current
                MsgBox("授权成功,已开始监听通知")
            Case UserNotificationListenerAccessStatus.Denied
                MsgBox("用户拒绝了通知访问权限,无法继续")
            Case UserNotificationListenerAccessStatus.Unspecified
                MsgBox("授权请求未完成,请重试")
        End Select
    End Sub

    Private Async Sub listen_NotificationChanged(sender As UserNotificationListener, e As UserNotificationChangedEventArgs) Handles listen.NotificationChanged
        ' 注意:事件回调可能不在UI线程,更新UI需要Invoke
        Me.Invoke(Sub()
                      MsgBox($"通知状态变更:{e.ChangeKind.ToString()}")
                  End Sub)
    End Sub
End Class

额外配置要求

你的WinForms项目需要启用Windows Runtime支持:

  • 右键项目 → 属性 → 应用程序 → 确认目标框架为.NET 6 (Windows)或.NET 7 (Windows)
  • 勾选“启用Windows Runtime支持”(若在属性面板找不到,可手动编辑项目文件,添加<UseWindowsRuntime>true</UseWindowsRuntime>到PropertyGroup节点中)

内容的提问来源于stack exchange,提问作者user9811991

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 06:25:21