[英].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.