Win Forms .NET 4.5中的Toast通知 [英] Toast Notifications in Win Forms .NET 4.5

查看:63
本文介绍了Win Forms .NET 4.5中的Toast通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了许多不同的帖子,这些内容与从Win Form创建Toast通知有关,但是当我通过这些帖子生成Toast通知时出现错误.

I've searched through a number of different posts to do with creating toast notifications from a Win Form however when these through I get an error when generating the toast notification.

System.Exception:找不到元素.(来自的例外HRESULT:0x80070490).

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

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

I have edited the csproj file and added the following:

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

并根据建议添加了对 Windows.Data Windows.UI 的引用以及对 System.Runtime.dll 的引用在 Windows.UI.Notifications

and added the references to Windows.Data and Windows.UI and also a reference to System.Runtime.dll as per the suggestions in Windows.UI.Notifications is missing

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());

            }
        }
    }
}

关于我要去哪里的任何建议吗?

Any suggestions as to where I am going wrong?

推荐答案

您应为CreateToastNotifier明确提供 applicationId .

You should explicitly provide applicationId for CreateToastNotifier.

赞:

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

但是我有个坏消息.从Windows 10 1709开始,WinForms应用程序仅不显示吐司通知.在Show(toast)正常运行之前,但现在它既不会引发异常,也不会显示任何Toast通知.

But I have bad news. Starting from Windows 10 1709 WinForms applications just does not show toast notifications. Before that Show(toast) was working, but now it neither throw an exception, nor show any toast notification.

我还在解决这个问题.

如Prateek Shrivastava所述,存在(新)限制.

As noted by Prateek Shrivastava there are (new) limitations.

在这里查看 https://docs.microsoft.com/en-us/uwp/api/windows.ui.notifications.toastnotificationmanager.createtoastnotifier

更新:
这是逐步指南,用于创建带有APP_ID的设置,因此通知将在所有所有 Windows 10版本上起作用:发送本地邮件桌面C#应用发出的吐司通知

Update:
Here is the step by step guide, to create a setup with APP_ID so notifications will work on all Windows 10 versions: Send a local toast notification from desktop C# apps

更新:
无需安装即可在Windows 10 1903中再次运行.

Update:
It works again in Windows 10 1903 without setup.

这篇关于Win Forms .NET 4.5中的Toast通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆