C# 控制台 Toast 停止显示通知
C# Console Toast Stops Displaying Notification
大约六个星期前,我开发了一个 C# 控制台应用程序作为生成 Windows 10 toast 通知的概念证明。工作正常。我重新访问该解决方案,发现它可以正确构建和运行,但 toast 通知不再显示。对 SO 进行了深入研究,到目前为止还没有爱。希望社区中有人能为我指明正确的方向,所以这里是:
环境
- Windows10,版本 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 通知。感谢您查看此内容。
此问题是由 Windows 10(版本 1709)的 Fall Creators Update 引起的。在 Windows10 的早期版本中,应用程序用户模型 ID (AppID) 几乎没有限制。但是,对于版本 1709,Microsoft 现在需要一个由 Windows 10 识别的现有 AppID 或通过 AppxManifest 或通过创建 Windows 开始菜单快捷方式创建的自定义 AppID,并通过 XML。我选择了现有的 Powershell AppId 并将我的代码更改为
private const string APP_ID = @"{1AC14E77-02E7-4E5D-B744-2EB1AE5198B7}\WindowsPowerShell\v1.0\powershell.exe";
当我开始研究如何使用 Powershell 祝酒时,我偶然发现了答案。这些链接更详细地介绍了问题及其解决方案:
GitHub Gist on Using Powershell AppId for Windows Toast
How to Create a Package Manifest Manually
更新
我最终使用 WiX 创建了自定义 Windows 开始菜单快捷方式,并通过 XML 将 AppID 嵌入其中。 (使用其他应用程序的 AppID 会在 Windows 操作中心中放置不正确的标签,以及其他问题)。我找到了一个非常好的博客 post,其中详细介绍了关键 XML 代码以及为快捷方式创建安装程序所需的步骤
Josh King: DIY AppId and WiX, for Tasty Toast
以及有关该主题的原始 WiX 文档
大约六个星期前,我开发了一个 C# 控制台应用程序作为生成 Windows 10 toast 通知的概念证明。工作正常。我重新访问该解决方案,发现它可以正确构建和运行,但 toast 通知不再显示。对 SO 进行了深入研究,到目前为止还没有爱。希望社区中有人能为我指明正确的方向,所以这里是:
环境
- Windows10,版本 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 通知。感谢您查看此内容。
此问题是由 Windows 10(版本 1709)的 Fall Creators Update 引起的。在 Windows10 的早期版本中,应用程序用户模型 ID (AppID) 几乎没有限制。但是,对于版本 1709,Microsoft 现在需要一个由 Windows 10 识别的现有 AppID 或通过 AppxManifest 或通过创建 Windows 开始菜单快捷方式创建的自定义 AppID,并通过 XML。我选择了现有的 Powershell AppId 并将我的代码更改为
private const string APP_ID = @"{1AC14E77-02E7-4E5D-B744-2EB1AE5198B7}\WindowsPowerShell\v1.0\powershell.exe";
当我开始研究如何使用 Powershell 祝酒时,我偶然发现了答案。这些链接更详细地介绍了问题及其解决方案:
GitHub Gist on Using Powershell AppId for Windows Toast
How to Create a Package Manifest Manually
更新
我最终使用 WiX 创建了自定义 Windows 开始菜单快捷方式,并通过 XML 将 AppID 嵌入其中。 (使用其他应用程序的 AppID 会在 Windows 操作中心中放置不正确的标签,以及其他问题)。我找到了一个非常好的博客 post,其中详细介绍了关键 XML 代码以及为快捷方式创建安装程序所需的步骤
Josh King: DIY AppId and WiX, for Tasty Toast
以及有关该主题的原始 WiX 文档