【问题标题】:WPF native windows 10 toastsWPF 本机 Windows 10 祝酒词
【发布时间】:2016-05-28 05:48:58
【问题描述】:

使用 .NET WPF 和 Windows 10,有没有办法使用 c# 将本地 toast 通知推送到操作中心?我只见过有人为此制作自定义对话框,但必须有一种方法可以通过操作系统来实现。

【问题讨论】:

  • 我实际上使用了那个库,现在我只需要找出方法在哪里;/
  • @AbinMathew Metro.Mahapps 与本地吐司无关。
  • @shady 据我记得,从 Win32 应用程序显示 toast 的唯一方法是使用 COM。
  • 供将来参考:已回答here

标签: wpf windows-10


【解决方案1】:

您可以像这样使用来自System.Windows.Forms 命名空间的NotifyIcon

class Test 
{
    private readonly NotifyIcon _notifyIcon;

    public Test() 
    {
        _notifyIcon = new NotifyIcon();
        // Extracts your app's icon and uses it as notify icon
        _notifyIcon.Icon = Icon.ExtractAssociatedIcon(Assembly.GetExecutingAssembly().Location);
        // Hides the icon when the notification is closed
        _notifyIcon.BalloonTipClosed += (s, e) => _notifyIcon.Visible = false;
    }

    public void ShowNotification() 
    {
        _notifyIcon.Visible = true;
        // Shows a notification with specified message and title
        _notifyIcon.ShowBalloonTip(3000, "Title", "Message", ToolTipIcon.Info);
    }

}

这应该从 .NET Framework 1.1 开始工作。 ShowBalloonTip的参数参考this MSDN page

我发现,ShowBalloonTip 的第一个参数(在我的示例中为 3000 毫秒)被大量忽略。评论表示赞赏;)

【讨论】:

  • 我必须指定System.Drawing.Icon.ExtractAssociatedIcon 而不是只指定Icon.ExtractAssociatedIcon 因为Icon 返回了当前的窗口图标 (WPF) .... 更重要的是 Visual Studio 没有自动建议更正
【解决方案2】:

我知道这是一篇旧帖子,但我认为这可能会像我在尝试让 Toast Notifications 在 Win 10 上工作时那样偶然发现此问题的人有所帮助。

这似乎是一个很好的大纲 - Send a local toast notification from desktop C# apps

我在这篇很棒的博客文章中使用了该链接-Pop a Toast Notification in WPF using Win 10 APIs

让我的 WPF 应用程序在 Win10 上运行。与“老派”通知图标相比,这是一个更好的解决方案,因为即使在通知进入操作中心之后,您也可以添加按钮来完成您的 toast 中的特定操作。

注意-第一个链接提到“如果您使用的是 WiX”,但这确实是一项要求。您必须先创建并安装您的 Wix 设置项目,然后 Toasts 才能工作。因为您的应用程序的 appUserModelId 需要先注册。除非您在其中阅读我的 cmets,否则第二个链接不会提及这一点。

提示- 安装应用后,您可以通过在运行行 shell:appsfolder 上运行此命令来验证 AppUserModelId。确保您在详细信息视图中,然后单击 ViewChoose Details 并确保选中 AppUserModeId。将您的 AppUserModelId 与其他已安装的应用进行比较。

这是我使用的代码片段。这里要注意两点,我没有安装第一个链接的第 7 步中提到的“通知库”,因为我更喜欢使用原始 XML。

private const String APP_ID = "YourCompanyName.YourAppName";

    public static void CreateToast()
    {
        XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(
            ToastTemplateType.ToastImageAndText02);

        // Fill in the text elements
        XmlNodeList stringElements = toastXml.GetElementsByTagName("text");
        stringElements[0].AppendChild(toastXml.CreateTextNode("This is my title!!!!!!!!!!"));
        stringElements[1].AppendChild(toastXml.CreateTextNode("This is my message!!!!!!!!!!!!"));

        // Specify the absolute path to an image
        string filePath = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86) + @"\Your Path To File\Your Image Name.png";
        XmlNodeList imageElements = toastXml.GetElementsByTagName("image");
        imageElements[0].Attributes.GetNamedItem("src").NodeValue = filePath;

        // Change default audio if desired - ref - https://docs.microsoft.com/en-us/uwp/schemas/tiles/toastschema/element-audio
        XmlElement audio = toastXml.CreateElement("audio");
        //audio.SetAttribute("src", "ms-winsoundevent:Notification.Reminder");
        //audio.SetAttribute("src", "ms-winsoundevent:Notification.IM");
        //audio.SetAttribute("src", "ms-winsoundevent:Notification.Mail"); // sounds like default
        //audio.SetAttribute("src", "ms-winsoundevent:Notification.Looping.Call7");  
        audio.SetAttribute("src", "ms-winsoundevent:Notification.Looping.Call2");
        //audio.SetAttribute("loop", "false");
        // Add the audio element
        toastXml.DocumentElement.AppendChild(audio);

        XmlElement actions = toastXml.CreateElement("actions");
        toastXml.DocumentElement.AppendChild(actions);

        // Create a simple button to display on the toast
        XmlElement action = toastXml.CreateElement("action");
        actions.AppendChild(action);
        action.SetAttribute("content", "Show details");
        action.SetAttribute("arguments", "viewdetails");

        // Create the toast 
        ToastNotification toast = new ToastNotification(toastXml);

        // Show the toast. Be sure to specify the AppUserModelId
        // on your application's shortcut!
        ToastNotificationManager.CreateToastNotifier(APP_ID).Show(toast);
    }

【讨论】:

    【解决方案3】:

    更新

    这似乎在 Windows 10 上运行良好

    https://msdn.microsoft.com/library/windows/apps/windows.ui.notifications.toastnotificationmanager.aspx

    您需要添加这些 nuget

    Install-Package WindowsAPICodePack-Core
    Install-Package WindowsAPICodePack-Shell
    

    【讨论】:

    • 您无法在 WPF 中执行此操作。
    • @Perfection 我认为这对 OP 来说应该没问题,你觉得呢?
    • 这看起来很有趣。我可以自己去拿这个。实际上我昨晚通过从资源工具包中引用 Windows.winmd 来解决它。
    • @HeadJ.E.M.你确定这适用于 WPF 桌面应用程序吗?
    • 问题是针对 WPF 而不是 UWP./UAP。
    【解决方案4】:

    添加参考:

    C:\Program Files (x86)\Windows Kits\8.1\References\CommonConfiguration\Neutral\Windows.winmd

    C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETCore\v4.5\System.Runtime.WindowsRuntime.dll

    并使用以下代码:

    XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastImageAndText04);
    
    // Fill in the text elements
    XmlNodeList stringElements = toastXml.GetElementsByTagName("text");
    for (int i = 0; i < stringElements.Length; i++)
    {
        stringElements[i].AppendChild(toastXml.CreateTextNode("Line " + i));
    }
    
    // Specify the absolute path to an image
    string imagePath = "file:///" + Path.GetFullPath("toastImageAndText.png");
    XmlNodeList imageElements = toastXml.GetElementsByTagName("image");
    
    ToastNotification toast = new ToastNotification(toastXml);
    ToastNotificationManager.CreateToastNotifier("Toast Sample").Show(toast);
    

    原始代码可以在这里找到:https://www.michaelcrump.net/pop-toast-notification-in-wpf/

    【讨论】:

      【解决方案5】:

      我设法通过引用获得了对 Windows 8 和 10 的工作 API 的访问权限

      • Windows.winmd: C:\ProgramFiles(x86)\WindowsKits\8.0\References\CommonConfiguration\Neutral

      这会暴露Windows.UI.Notifications

      【讨论】:

      • 我在那个目录中没有那个 dll :/
      【解决方案6】:

      您可以查看这篇文章,了解如何创建一个 COM 服务器,以便使用 Win32 应用程序将通知保留在 AC 中 https://blogs.msdn.microsoft.com/tiles_and_toasts/2015/10/16/quickstart-handling-toast-activations-from-win32-apps-in-windows-10/

      可以在https://github.com/WindowsNotifications/desktop-toasts找到工作示例

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多