Win Forms .NET 4.5 中的 Toast 通知

12
我已搜索了许多关于从Win表单创建Toast通知的帖子,但是当我执行这些操作时,在生成Toast通知时出现错误。

System.Exception: Element not found. (Exception from HRESULT:0x80070490).

我已编辑csproj文件并添加以下内容:

  <PropertyGroup>
       <TargetPlatformVersion>10.0.10586</TargetPlatformVersion>
  </PropertyGroup>

根据Windows.UI.Notifications is missing的建议,添加了对Windows.DataWindows.UI的引用以及对System.Runtime.dll的引用。

using Windows.Data.Xml.Dom;
using Windows.UI.Notifications;
using System.Windows.Forms;
using System;

namespace ToastNotify
{
    class Notify
    {
        public void GenerateToast(string header, string content)
        {
            ToastTemplateType toastTemplate = ToastTemplateType.ToastImageAndText02;

            XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(toastTemplate);

            XmlNodeList toastTextElements = toastXml.GetElementsByTagName("text");
            toastTextElements[0].AppendChild(toastXml.CreateTextNode(header));
            toastTextElements[1].AppendChild(toastXml.CreateTextNode(content));

            XmlNodeList toastImageElements = toastXml.GetElementsByTagName("image");
            ((XmlElement)toastImageElements[0]).SetAttribute("src", "..\\..\\Resources\\icon.ico");

            IXmlNode toastNode = toastXml.SelectSingleNode("/toast");
            ((XmlElement)toastNode).SetAttribute("duration", "long");

            ToastNotification toast = new ToastNotification(toastXml);

            try
            {
                ToastNotificationManager.CreateToastNotifier().Show(toast);
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());

            }
        }
    }
}

有关我做错了哪些方面的任何建议?


元素未找到 - 异常意味着 - 您尝试查找的某个 XML 元素不在 Xml 中(或者不在您期望的 xpath 中)。 - Prateek Shrivastava
1
有趣。顺便提一下,这段代码在创建新的通用 Windows 应用程序时可以工作,但显然无法在 Win 表单中引用 .net core 类。 - Etterz
异常抛出于:ToastNotificationManager.CreateToastNotifier(),Show(toast); - Etterz
1
AppUserModelId就是缺失的那个部分!谢谢! - Etterz
https://dev59.com/oFkT5IYBdhLWcg3wD7Vb - gcode
显示剩余4条评论
3个回答

10

你应该明确提供 applicationId 以便创建ToastNotifier。

像这样:

private const String APP_ID = "Microsoft.Samples.DesktopToastsSample";
...
ToastNotificationManager.CreateToastNotifier(APP_ID).Show(toast);

但是我有一个坏消息。从Windows 10 1709开始,WinForms应用程序就不能显示toast通知了。在此之前,Show(toast)是有效的,但现在它既不会抛出异常,也不会显示任何toast通知。
我仍在研究这个问题。
正如Prateek Shrivastava所指出的,存在(新的)限制。
请参阅此处 https://learn.microsoft.com/en-us/uwp/api/windows.ui.notifications.toastnotificationmanager.createtoastnotifier 更新:
这里是逐步指南,创建包含APP_ID的设置,以便通知可以在所有Windows 10版本上工作:从桌面C#应用程序发送本地toast通知 更新:
在Windows 10 1903中,它又可以工作了,无需设置。

1
如果我正在开发本地解决方案,我应该从哪里获取applicationId? - Ali123
1
@Ali123 应用程序 ID 是自定义的。你可以自己编写。这是你用来注册应用程序的字符串,所以他们建议使用“公司名称/产品名称”,以避免冲突。最终,虽然上面提到的 MS 示例链接似乎相当完整,但它是垃圾。无法工作。 - The Muffin Man

1

使用此代码,并确保您设置了图像(图标)的完整路径,如果要显示图标,则传递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);
}

0

我需要在这里插话,因为我正在开发一个WinForms应用程序(.Net Framework 4.7*),但在GitHub构建工作器上无法构建,因为在某个时候,支持toast通知被黑客入侵,并引用了一些奇怪的WinMetadata\Windows.*.winmd文件。

谁要是找到了这个StackOverflow问题就倒霉了。

你可能正在针对.Net Framework 4.x进行开发,因为你正在开发一个Win Forms应用程序。.Net Framework 4.8受支持在Windows 7上,而Windows 7没有ToastNotifications,这可能是你正在寻找的类。因此出现了问题。

我认为唯一的解决方案是在更新的答案中最受欢迎的那个, 它只是针对更现代的UWP框架,这将仅适用于Windows 10及以上版本。但你仍然试图从一个不支持它的框架发送toast通知。在你的代码的某个地方,你可能正在检查你运行的平台是否是Windows 10或更新版本。甚至不是编译器语句,只是纯粹的C#或VB.Net。这不是.Net Framework设计的工作方式 - 它的目的是跨平台。

我的唯一建议是; 为Windows 10+设置一个新的构建目标,并按照Stephen的建议设置你的项目。这应该是让WinForms做出漂亮的Windows 10技巧的最少hackish的方法。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接