【问题标题】:Notifications on Windows 10 using desktop app使用桌面应用程序在 Windows 10 上通知
【发布时间】:2014-11-29 07:15:06
【问题描述】:

我在 C# 中获得了一个带有 NotifyIcon 的 Windows 窗体项目。我正在使用以下代码来显示气球通知:

notifyIcon.ShowBalloonTip(1000, "title", "text", ToolTipIcon.Info);

这在 Windows 8.1 之前运行良好。现在我已经安装了 Windows 10 Preview,气球通知不再显示了。

我猜所有的通知都被移到了 Windows 8 风格的 toast 通知中,并且气球通知被完全删除了(因为我还没有看到一个气球通知和更多的 toast 通知),但是我还没有找到一个官方来源尚未。

问题是我的应用程序只是一个 .exe 文件,因此它没有安装程序或快捷方式。根据this 页面,需要创建快捷方式的安装程序才能运行 toast 通知。

如何在没有任何快捷方式或安装程序的情况下在 Windows 10 中显示通知(我不在乎它是气球通知还是 toast 通知)?

【问题讨论】:

  • 许多应用程序似乎只使用 toast 通知而没有任何代码更新,但我猜这是因为它们已使用安装程序安装并具有快捷方式。我认为微软的开发人员只是在将气球通知重定向到 toast 通知时忘记了他们自己关于 toast 通知的规则。或者他们可能是故意的。我希望它会很快得到解决,或者至少能得到一些关于这个问题的信息
  • 我写了一篇关于在桌面应用程序中使用 WinRT api 的博文,例如显示 Toast 通知:meziantou.net/2017/09/18/…。基本思路是在csproj中添加<TargetPlatformVersion>8.0</TargetPlatformVersion>,并添加对Windows的引用。然后就可以使用WinRT api了,比如ToastNotificationManager.CreateToastNotifier。

标签: c# windows winforms notifications


【解决方案1】:

我使用此线程来帮助编写此代码 - 我正在分享我的成功。

警告我是 C# 的新手,所以我的代码可能很糟糕——但它确实有效,而且非常简单,对于我找到的大多数解决方案来说,这比我能说的要多

另外,我在阅读 xml 文档时也遇到了麻烦。我正在与 System.xml(我认为)和 Windows.Data.Dom.Xml(也不完全确定)作斗争。 最后,我决定为我的示例文件制作硬编码字符串,并使用 switch 语句在它们之间切换。 我找到了很多人,在堆栈溢出时寻找我想出的解决方案。似乎将 toast 通知系统与控制台或后台应用程序一起使用会非常有用,并且围绕带有 windows 应用程序的 toast 通知系统的文档都表明它需要与应用程序一起使用。操作中心对于通知与 NotificationTray/NotifyIcon 路由非常有用。我在网络上的其他任何地方都没有找到完整的解决方案。这是示例代码。

/*
At first you need to declare that your program will be using winRT libraries:
1. Right click on your yourProject, select Unload Project
2. Right click on your youProject(unavailable) and click Edit yourProject.csproj
3. Add a new property group:<TargetPlatformVersion>8.0</TargetPlatformVersion>
4. Reload project
5. Add referece Windows from Windows > Core
*/
using System;
using Windows.Data.Xml.Dom;
using Windows.Storage;
using Windows.Storage.Streams;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Notifications;

namespace ConsoleApplication6
{
    public class NewToastNotification
    {
        public NewToastNotification(string input, int type)
        {
            string NotificationTextThing = input;
            string Toast = "";
            switch (type)
            {
                case 1:
                    {
                        //Basic Toast
                        Toast = "<toast><visual><binding template=\"ToastImageAndText01\"><text id = \"1\" >";
                        Toast += NotificationTextThing;
                        Toast += "</text></binding></visual></toast>";
                        break;
                    }
                default:
                    {
                        Toast = "<toast><visual><binding template=\"ToastImageAndText01\"><text id = \"1\" >";
                        Toast += "Default Text String";
                        Toast += "</text></binding></visual></toast>";
                        break;
                    }
            }
            XmlDocument tileXml = new XmlDocument();
            tileXml.LoadXml(Toast);
            var toast = new ToastNotification(tileXml);
            ToastNotificationManager.CreateToastNotifier("New Toast Thing").Show(toast);
        }
}

    class Program
    {
        static void Main(string[] args)
        {
            NewToastNotification Window = new NewToastNotification("Yes",1);


        }
    }
}

【讨论】:

  • 对非 Win10 应用无用!
  • 在 win8+ 中工作,我相信这是实现磁贴的时候
【解决方案2】:

这个解决方案对我有用:

  • 打开 gpedit.msc
  • 用户配置 -> 管理模板 -> 开始菜单和任务栏 -> “禁用将气球通知显示为 toast” 设置为“启用”
  • 重新启动 Windows

【讨论】:

    【解决方案3】:

    好吧,在技术预览的最后一次更新(系统托盘旁边有一个通知中心的那个)之后,它正在工作。

    我的通知像往常一样显示,没有任何代码更改。它们就像普通的 Windows 8 样式的 Toast 通知一样,只是它们显示在底部。

    【讨论】:

      猜你喜欢
      • 2015-04-20
      • 2018-05-27
      • 1970-01-01
      • 2015-12-23
      • 2018-09-10
      • 2017-02-06
      • 1970-01-01
      • 2016-11-05
      • 1970-01-01
      相关资源
      最近更新 更多