在Windows 10上使用桌面应用程序的通知

7
我是一位有用的助手,可以为您翻译文本。

我在 C# 中有一个 Windows Forms 项目,其中包含一个 NotifyIcon。我正在使用以下代码显示气球通知:

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

直到Windows 8.1之前,这一切都运行良好。现在我安装了Windows 10 Preview,气球通知不再显示。我猜所有的通知都被移动到了Windows 8风格的吐司通知中,并且气球通知完全被删除了(因为我还没有看到过单个气球通知,但是有很多吐司通知),但是我还没有找到官方来源。问题是我的应用程序只是一个单独的.exe文件,因此它没有安装程序或快捷方式。根据this页面,需要创建快捷方式的安装程序才能使吐司通知起作用。如何在没有任何快捷方式或安装程序的情况下在Windows 10中显示通知(无论是气球还是吐司通知)?

许多应用程序似乎只是使用了 toast 通知,而没有进行任何代码更新,但我猜这是因为它们已经使用安装程序安装并有快捷方式。当微软将气球通知重定向到 toast 通知时,我认为 Microsoft 的开发人员忘记了它们自己的 toast 通知规则。或者他们是有意这样做的。我希望这个问题能够尽快得到解决,或者至少提供一些关于问题的信息。 - Louis Matthijssen
我写了一篇关于如何在桌面应用程序中使用WinRT API的博客文章,例如显示Toast通知:https://www.meziantou.net/2017/09/18/display-toast-notifications-in-a-wpf-application。基本思路是在csproj中添加`<TargetPlatformVersion>8.0</TargetPlatformVersion>并添加对Windows的引用。然后,您可以使用WinRT api,例如ToastNotificationManager.CreateToastNotifier`。 - meziantou
3个回答

1

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

警告:我对C#还比较新,所以我的代码可能很糟糕 - 但它确实有效,并且相当简单,这比我找到的大多数解决方案都要好

另外,我一直在努力让xml文档读取。我曾经尝试使用System.xml(我想是)和Windows.Data.Dom.Xml(也不完全确定)来处理。最终,我将它们设置为了示例文件的硬编码字符串,并使用switch语句在它们之间进行切换。 我在stackoverflow上发现了很多人寻找我提出的解决方案。似乎将Toast通知系统与控制台或后台应用程序一起使用非常有用,并且围绕Windows应用程序的Toast通知系统的文档都建议必须与应用程序一起使用。Action Center对于通知而言比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应用程序无用! - Martin.Martinsson
适用于Win8+,我相信这是在实现平铺式界面之后。 - probsJustin

0

这个解决方案对我有效:

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

0

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

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


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