C# 在.Net5.0中使用ToastNotificationManager

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) {

我使用下面的代码来显示来自.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)
        {
            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或更高版本操作系统的项目中受支持。有关此场景的更多背景信息,请参阅本文

  • 在Visual Studio中打开项目后,在中右键单击项目 解决方案资源管理器,然后选择“编辑项目文件”。您的项目文件 应该看起来像这样

    温克斯 net5.0 真的
  • 将TargetFramework元素的值替换为 以下字符串:

    net5.0-windows10.0.17763.0:如果应用程序的目标是 Windows10,版本1809。net5.0-windows10.0.18362.0:使用此值 如果您的应用程序针对Windows 10,版本1903。 net5.0-windows10.0.19041.0:如果应用程序目标为 Windows 10,版本2004

    例如,以下元素适用于目标为 Windows 10,版本2004

    net5.0-windows10.0.19041.0

  • 保存更改并关闭项目文件


  • WinRT支持认为,如果您可以标记您的答案,以便其他有相同问题的人可以参考此案例,则效果更好。