首页 > 解决方案 > C# .NetCore windows 通知?

问题描述

有没有办法从 .NetCore(3.1 版)程序中获取弹出菜单或侧边栏通知?

我尝试了 MessageBox 和 ToastNotification 等多种解决方案,但它们都不起作用。MessageBox 不允许我添加“使用”(不知何故它应该是可能的,但是它加载了一个多小时并且没有发生任何事情)并且 ToastNotification 只是没有给我任何回报....就像我没有收到错误,也没有通知和调试没有告诉我任何有用的价值...

在此处输入图像描述

标签: c#.net-corenotificationspopup

解决方案


所以 ToastContentBuilder 不知何故不起作用。我假设 Builder 应该创建一个 XML,然后将其放入 ToastNotification。使用 .NetCore 3.1 不起作用(也许在其他一些框架中它可以工作)。所以解决这个问题的方法是自己创建 XML 文件。所以这里有一个人人都可以使用的功能。唯一需要编辑才能在其他代码中工作的是 .CreateToast 通知程序。您必须在此处输入项目的名称。

 public static void showToastNotification(string toastText)
    {
        XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText01);

        XmlNodeList stringElements = toastXml.GetElementsByTagName("text");

        for (int i = 0; i < stringElements.Length; i++)
        {
            stringElements[i].AppendChild(toastXml.CreateTextNode(toastText));
        }

        ToastNotification toast = new ToastNotification(toastXml);
        toast.Activated += Toast_Activated;
        toast.Dismissed += Toast_Dismissed;
        toast.Failed += Toast_Failed;

        ToastNotificationManager.CreateToastNotifier("'Your project name'").Show(toast);
    }

推荐阅读