C# 在.Net5.0中使用ToastNotificationManager
我使用下面的代码来显示来自.NetCore 3.1控制台应用程序的Windows 10 Toast通知,我使用了来自以下命名空间的对象:C# 在.Net5.0中使用ToastNotificationManager,c#,uwp,.net-5,C#,Uwp,.net 5,我使用下面的代码来显示来自.NetCore 3.1控制台应用程序的Windows 10 Toast通知,我使用了来自以下命名空间的对象:Windows.UI.notifications&Windows.Data.Xml.Dom,在.Net5.0中,这些命名空间似乎被移动到了其他地方 public void GenerateToast(string appid, string imageFullPath, string h1, string h2, string p1) {
Windows.UI.notifications
&Windows.Data.Xml.Dom
,在.Net5.0中,这些命名空间似乎被移动到了其他地方
public void GenerateToast(string appid, string imageFullPath, string h1, string h2, string p1)
{
try
{
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);
}
catch (Exception)
{
// Ignore
}
}
如何取回这些名称空间?找到了解决方案:
此选项仅在使用.NET 5(或更高版本)和目标Windows 10、版本1809或更高版本操作系统的项目中受支持。有关此场景的更多背景信息,请参阅本文
WinRT支持认为,如果您可以标记您的答案,以便其他有相同问题的人可以参考此案例,则效果更好。