C#控制台吐司停止显示通知

时间:2018-06-20 21:41:49

标签: c# windows winapi console-application toast

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

环境

  • Windows 10版本1709
  • .NET Framework 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的生成(是否将File.WriteAllText与通过content.GetContent()获得的XML一起使用),并且Toast XML可以很好地生成。似乎是某种原因阻止了ToastNotificationManager.CreateToastNotifier显示敬酒通知。感谢您的关注。

1 个答案:

答案 0 :(得分:3)

此问题是由Windows 10的Fall Creators Update(版本1709)引起的。在Windows 10的早期版本中,应用程序用户模型ID(AppID)几乎没有限制。但是,在版本1709中,Microsoft现在需要Windows 10识别的现有AppID或通过AppxManifest或通过创建Windows Start Menu快捷方式创建的自定义AppID,该快捷方式中的AppId通过XML嵌入其中。我选择了现有的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“开始”菜单快捷方式,并通过XML嵌入了其中的AppID。 (除其他问题外,使用另一个应用程序的AppID在Windows Action Center中放置了错误的标签)。我找到了一篇很好的博客文章,其中详细介绍了关键XML代码以及为快捷方式创建安装程序所需的步骤

Josh King: DIY AppId and WiX, for Tasty Toast

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

WiX How To: Create a Shortcut on the Start Menu

相关问题