【问题标题】:C# Console Toast Stops Displaying NotificationC# 控制台 Toast 停止显示通知
【发布时间】:2018-06-20 21:41:49
【问题描述】:

大约六周前,我开发了一个 C# 控制台应用程序作为生成 Windows 10 toast 通知的概念证明。工作得很好。我重新访问该解决方案只是发现它构建并运行没有错误,但不再显示 toast 通知。对SO进行了彻底的研究,到目前为止还没有爱。希望社区中的某个人可以为我指明正确的方向,所以这里是:

环境

  • Windows 10 版本 1709
  • .NET 框架 4.6.1
  • Visual Studio 2017,版本 15.7.4

.csproj 文件添加

<PropertyGroup> <TargetPlatformVersion>10.0.1709</TargetPlatformVersion> </PropertyGroup>

添加的参考文献

  • Windows.Data
  • Windows.UI
  • System.Runtime(来自 .NETFramework\v4.6.1\Facades)

Program.cs

using System;
using System.IO;
using Microsoft.Toolkit.Uwp.Notifications;
using Windows.UI.Notifications;
using Windows.Data.Xml.Dom;

class Program
{
    private const String APP_ID = "CompanyName.Notifier.ToastName";

    static void Main(string[] args)
    {
        ToastContent content = new ToastContent()
        {
            Visual = new ToastVisual()
            {
                BindingGeneric = new ToastBindingGeneric()
                {
                    Children =
                    {
                        new AdaptiveText()
                        {
                            Text = "Desired text",
                            HintMaxLines = 1
                        },

                        new AdaptiveText()
                        {
                            Text = "More desired text"
                        },
                    },

                    HeroImage = new ToastGenericHeroImage()
                    {
                        Source = @"C:\Logo.png" // hard-coded path for testing
                    }
                }
            },

            Duration = ToastDuration.Long

        };

        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.LoadXml(content.GetContent());

        var tstVisual = new ToastNotification(xmlDoc);

        ToastNotificationManager.CreateToastNotifier(APP_ID).Show(tstVisual);

    }
}

其他想法

我检查了 XML 的生成(使用content.GetContent() 获得的 XML 进行了File.WriteAllText)并且 toast XML 生成正常。似乎有什么东西阻止了 ToastNotificationManager.CreateToastNotifier 显示 toast 通知。感谢您查看此内容。

【问题讨论】:

  • 您能否检查一下您的源代码控制系统并向我们展示最近对代码所做的任何更改? content.GetContent() 的值是多少?
  • Main方法的末尾添加Console.ReadLine();有帮助吗?
  • @mjwills 未对代码进行任何更改。该解决方案甚至没有检入 TFS(因为它是 POC)。 Console.ReadLine(); 没有影响。仅供参考,应用程序属性输出类型为Windows Application,以防止显示控制台窗口。
  • 你试过在另一台机器上运行它吗?也许 Windows 10 安装了某种更新,而您却没有发现它破坏了您的代码。
  • @PaulSanders 我确实在另一台 Windows PC 上尝试过我的代码。没有销售。现在仍然有可能 Windows 更新确实占用了我的代码,但是我可以访问的所有 PC 都有相同的更新,我无权在我的公司桌面上回滚它们。

标签: c# windows winapi console-application toast


【解决方案1】:

此问题是由 Windows 10(版本 1709)的 Fall Creators Update 引起的。在 Windows 10 的早期版本中,应用程序用户模型 ID (AppID) 几乎没有限制。但是,在版本 1709 中,Microsoft 现在需要 Windows 10 识别的现有 AppID 或通过 AppxManifest 创建的自定义 AppID 或通过创建 Windows 开始菜单快捷方式并通过 XML 嵌入其中的 AppId。我选择了现有的 Powershell AppId 并将我的代码更改为

private const string APP_ID = @"{1AC14E77-02E7-4E5D-B744-2EB1AE5198B7}\WindowsPowerShell\v1.0\powershell.exe";

当我开始研究如何使用 Powershell 祝酒时,我偶然发现了答案。这些链接更详细地介绍了该问题及其解决方案:

Toast Notification Not Working on Windows Fall Creators Update

GitHub Gist on Using Powershell AppId for Windows Toast

How to Create a Package Manifest Manually

更新

我最终使用 WiX 创建了一个自定义 Windows 开始菜单快捷方式,其中 AppID 通过 XML 嵌入其中。 (使用另一个应用程序的 AppID 会在 Windows 操作中心中放置不正确的标签,以及其他问题)。我找到了一篇非常好的博文,详细介绍了关键的 XML 代码以及为快捷方式创建安装程序所需的步骤

Josh King: DIY AppId and WiX, for Tasty Toast

以及有关该主题的原始 WiX 文档

WiX How To: Create a Shortcut on the Start Menu

【讨论】:

    猜你喜欢
    • 2015-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-29
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    相关资源
    最近更新 更多