[英].NetCore Show Windows 10 Notification (Toast)
我已经搜索了许多关于如何从 .netCore 应用程序创建 toast 通知的不同帖子,但是,它们都没有帮助微软的错误文档。
很高兴在这里获得有关如何显示 Windows 10 通知 (Toast) 的完整答案以及来自 .NetCore 控制台应用程序的图像?
首先,确保您的目标不是 .NET 5.0 - 此框架不受支持(目前)。
然后,安装Microsoft.Windows.SDK.Contracts
NuGet ZEFE90A8E604A7C840E88D03A6
如果要显示图标,请使用此选项并确保设置图像(图标)完整路径,否则只需通过 null。
public static void GenerateToast(string appid, string imageFullPath, string h1, string h2, string p1)
{
var template = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastImageAndText04);
var textNodes = template.GetElementsByTagName("text");
textNodes[0].AppendChild(template.CreateTextNode(h1));
textNodes[1].AppendChild(template.CreateTextNode(h2));
textNodes[2].AppendChild(template.CreateTextNode(p1));
if (File.Exists(imageFullPath))
{
XmlNodeList toastImageElements = template.GetElementsByTagName("image");
((XmlElement)toastImageElements[0]).SetAttribute("src", imageFullPath);
}
IXmlNode toastNode = template.SelectSingleNode("/toast");
((XmlElement)toastNode).SetAttribute("duration", "long");
var notifier = ToastNotificationManager.CreateToastNotifier(appid);
var notification = new ToastNotification(template);
notifier.Show(notification);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.