繁体   English   中英

在.Net5.0中使用ToastNotificationManager

[英]Using ToastNotificationManager in .Net5.0

I was using the following code to show a Windows 10 Toast notifications from a.NetCore 3.1 console application, where I was using objects from the following namespaces: Windows.UI.Notifications & Windows.Data.Xml.Dom , in.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 或更高操作系统版本为目标的项目中受支持。 有关此方案的更多背景信息,请参阅此博客文章。

  1. 在 Visual Studio 中打开您的项目后,在解决方案资源管理器中右键单击您的项目并选择编辑项目文件。 您的项目文件应与此类似。

    WinExe net5.0 真
  2. 将 TargetFramework 元素的值替换为以下字符串之一:

    net5.0-windows10.0.17763.0:如果您的应用针对 Windows 10,版本 1809,请使用此值。 net5.0-windows10.0.18362.0:如果您的应用针对 ZAEA23489CE3AA9B6406EBB20,版本 18E0CDA430Z51010,请使用此值。 windows10.0.19041.0:如果您的应用面向 Windows 10,版本 2004,则使用此值。

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

    net5.0-windows10.0.19041.0

  3. 保存更改并关闭项目文件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM