【问题标题】:C# - UWP AppInfo throws NotImplementedExceptionC# - UWP AppInfo 抛出 NotImplementedException
【发布时间】:2021-06-30 16:28:51
【问题描述】:

我正在使用 UWP API 开发 C# WinForms 应用程序。我正在尝试以编程方式阅读通知,到目前为止我已经成功了。但是,每当我从 UserNotification 类调用 AppInfo 时,我都会收到 NotImplementedException,无论我从 AppInfo 读取什么属性。

有人有什么建议吗?

我只能找到这个问题的 1 个答案,它不是很有用,而且也有几年的历史了。这是我项目中的主要障碍,非常感谢任何帮助!

提前谢谢你。

编辑

这是我的代码。

        try {
            this.source = notification.AppInfo.DisplayInfo.DisplayName;
        } catch(NotImplementedException e) {
            this.source = "Unspecified";
        }

        NotificationBinding binding = notification.Notification.Visual.GetBinding(KnownNotificationBindings.ToastGeneric);
        if (binding != null) {
            Console.WriteLine(binding.GetTextElements()[1]);
            this.title = binding.GetTextElements().FirstOrDefault()?.Text;
            this.body = string.Join("\n", binding.GetTextElements().Skip(1).Select(t => t.Text));
        }
        Init();

我正在使用来自the examples in the docs.的代码

【问题讨论】:

  • 请发布您的代码。您很可能需要将包含 NotImplementedException 的行替换为一些实际代码(或将其注释掉)。
  • 就像我说的,我正在使用 UWP API。它可以很好地读取通知,我不确定如果异常不是由我的代码特别引起的,我将如何处理它。

标签: c# windows winforms uwp


【解决方案1】:

UWP AppInfo 抛出 NotImplementedException

根据异常消息,notification.AppInfo.DisplayInfo 似乎没有 已针对 WinForm 平台实施。对于这种情况,我们有一个使用 [AppDiagnosticInfo][1] api 获取 AppInfo 的解决方法。请参考以下代码

var list = await AppDiagnosticInfo.RequestInfoAsync();
var currentPackage = list.Where(o => o.AppInfo.PackageFamilyName == Package.Current.Id.FamilyName).FirstOrDefault();
if (currentPackage != null)
{
    AppInfo currentAppInfo = currentPackage.AppInfo;
    var display = currentAppInfo.DisplayInfo;
}

【讨论】:

  • 这不会得到我的程序的显示名称吗?这不是我想要做的,如果我解释得不够彻底,我很抱歉。我需要发送任何通知的程序的名称。这将包括谷歌浏览器、截图工具、Windows Defender。我正在使用通知侦听器来捕获任何和所有通知,除了 UserNotification 的 AppInfo 属性之外,它工作得很好。
  • 你在使用UserNotificationListener Class吗?如果是,则可能是因为不支持在非 UWP 应用中使用 UserNotificationListener 类。
猜你喜欢
  • 1970-01-01
  • 2016-05-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-06
相关资源
最近更新 更多