繁体   English   中英

可以从非 UWP C# 应用程序在 Windows 10 中发送 Toast 通知吗?

[英]Possible to send Toast Notification in Windows 10 from a non-UWP C# app?

Windows 10 中的 Toast 通知很棒,有许多为通用 Windows 平台 (UWP) 应用程序编写的 C# 代码示例,但我找不到为非 UWP(.NET 框架中的 Windows 经典桌面)编写的任何示例。

我想在我的下一个 C# 项目中使用此功能,但想在非 UWP C# 中对其进行编码,这可能吗? 谁能给我一些代码示例?

要从Windows 10中的非UWP应用发送Toast通知,请参阅快速入门:从Windows 10中的Win32应用处理Toast激活

这将向您展示如何注册COM服务器,以便用户可以单击您的祝酒词来启动您的应用程序(由于Toasts保留在Action Center中,因此即使在您关闭应用程序的情况下,也可以随时单击并启动它们)。

您可以使用嵌入在 c# 中的 powershell。 这在依赖很少的情况下效果很好,甚至可以在 ssis 中运行。


using System.IO;
using System.Configuration;
using System.Management.Automation;

try
{

    // run powershell script
    string scriptDirectory = Path.GetDirectoryName(
            ConfigurationManager.AppSettings["PathToTechOpsTooling"]);

    var script = @"
    $ErrorActionPreference = ""Stop""
    $notificationTitle = ""Notification: Your script:" + popupMessage + @" has been completed successfully""
        [Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] > $null
    $template = [Windows.UI.Notifications.ToastNotificationManager]::GetTemplateContent([Windows.UI.Notifications.ToastTemplateType]::ToastText01)
    $toastXml = [xml] $template.GetXml()
    $toastXml.GetElementsByTagName(""text"").AppendChild($toastXml.CreateTextNode($notificationTitle)) > $null
    $xml = New-Object Windows.Data.Xml.Dom.XmlDocument
    $xml.LoadXml($toastXml.OuterXml)
    $toast = [Windows.UI.Notifications.ToastNotification]::new($xml)
    $toast.Tag = ""Test1""
    $toast.Group = ""Test2""
    $toast.ExpirationTime = [DateTimeOffset]::Now.AddSeconds(15)
    $notifier = [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier(""Script Completed!"")
    $notifier.Show($toast);
    ";

    var _powershell = PowerShell.Create().AddScript(script);
    _powershell.Invoke();
    foreach (var errorRecord in _powershell.Streams.Error)
        Console.WriteLine(errorRecord);

}
catch (System.Exception ex)
{
    Console.WriteLine(ex.ToString());

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM