是否可以从控制台应用程序发送Toast通知?

Eva*_*s B 13 c# windows notifications console-application win-universal-app

是否可以使用ToastNotificationManager从控制台应用程序发送Toast通知?

我知道可以从Windows Universal app发送Toast通知:

var toast = new ToastNotification(doc);
ToastNotificationManager.CreateToastNotifier().Show(toast);
Run Code Online (Sandbox Code Playgroud)

*doc - 存储在XML字符串中的Toast

要使用ToastNotificaionManager,我需要Windows.UI.Notifications库,我无法在控制台应用程序项目中引用它.

我之前提到的库实际上是由WinRT使用的.是否可以在Windows控制台应用程序中使用WinRT API?

Eva*_*s B 16

首先,您需要声明您的程序将使用winRT库:
1.右键单击您的项目,选择卸载项目
2.右键单击您的项目(不可用),然后单击编辑yourProject.csproj
3.添加新属性组:<targetplatformversion>8.0</targetplatformversion>
4.重新加载项目
5. 从Windows> Core添加引用Windows
在此输入图像描述

现在您需要添加以下代码:

using Windows.UI.Notifications;
Run Code Online (Sandbox Code Playgroud)

并且您将能够使用此代码发送通知:

var toast = new ToastNotification(doc);
ToastNotificationManager.CreateToastNotifier().Show(toast);
Run Code Online (Sandbox Code Playgroud)

参考:如何从C#桌面应用程序--WindRT Diagram中调用Windows 8中的WinRT API


pro*_*tin 5

我在 Evaldas B 的代码中遇到了一些问题,我缺少一个字符串。(这里说需要字符串的地方)

.CreateToastNotifier(<needed a string here>).Show(toast);

警告我是 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);


        }
    }
}
Run Code Online (Sandbox Code Playgroud)