我已经搜索了许多关于如何从.netCore应用程序创建吐司通知的不同帖子,然而,它们都无助于Microsofts的buggy文档。
因此,在这里得到一个完整的答案,如何显示带有来自.NetCore控制台应用程序的图像的Windows10通知(Toast)?
发布于 2020-11-28 21:21:28
首先,确保您没有针对.NET 5.0 --这个框架还不支持(目前)。
然后,安装Microsoft.Windows.SDK.Contracts NuGet包。
如果您想要显示图标,请使用此选项,并确保您正在设置图像(图标)完整路径,否则只需传递空。
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);
}https://stackoverflow.com/questions/65054564
复制相似问题