WPF 本机 Windows 10 吐司

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35441202/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:40:38  来源:igfitidea点击:

WPF native windows 10 toasts

wpfwindows-10

提问by shady

Using .NET WPF and Windows 10, is there a way to push a local toast notification onto the action center using c#? I've only seen people making custom dialogs for that but there must be a way to do it through the os.

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

回答by Franz Wimmer

You can use a NotifyIconfrom System.Windows.Formsnamespace like this:

您可以像这样使用NotifyIconfromSystem.Windows.Forms命名空间:

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);
    }

}

This should work since .NET Framework 1.1. Refer to this MSDN pagefor parameters of ShowBalloonTip.

这应该从 .NET Framework 1.1 开始工作。请参阅此 MSDN 页面了解ShowBalloonTip.

As I found out, the first parameter of ShowBalloonTip(in my example that would be 3000 milliseconds) is generously ignored. Comments are appreciated ;)

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

回答by Kevin Moore

I know this is an old post but I thought this might help someone that stumbles on this as I did when attempting to get Toast Notifications to work on Win 10.

我知道这是一篇旧帖子,但我认为这可能会帮助那些在尝试让 Toast 通知在 Win 10 上工作时偶然发现的人。

This seems to be good outline to follow - Send a local toast notification from desktop C# apps

这似乎是一个很好的大纲 - 从桌面 C# 应用程序发送本地 Toast 通知

I used that link along with this great blog post- Pop a Toast Notification in WPF using Win 10 APIs

我使用该链接以及这篇很棒的博客文章-使用 Win 10 API 在 WPF 中弹出 Toast 通知

to get my WPF app working on Win10. This is a much better solution vs the "old school" notify icon because you can add buttons to complete specific actions within your toasts even after the notification has entered the action center.

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

Note- the first link mentions "If you are using WiX" but it's really a requirement. You must create and install your Wix setup project before you Toasts will work. As the appUserModelId for your app needs to be registered first. The second link does not mention this unless you read my comments within it.

注意 - 第一个链接提到“如果您使用 WiX”,但这确实是一个要求。在 Toasts 工作之前,您必须创建并安装您的 Wix 安装项目。因为您的应用的 appUserModelId 需要先注册。第二个链接没有提到这一点,除非您阅读我在其中的评论。

TIP- Once your app is installed you can verify the AppUserModelId by running this command on the run line shell:appsfolder. Make sure you are in the details view, next click View, Choose Detailsand ensure AppUserModeId is checked. Compare your AppUserModelId against other installed apps.

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

Here's a snipit of code that I used. One thing two note here, I did not install the "Notifications library" mentioned in step 7 of the first link because I prefer to use the raw XML.

这是我使用的代码片段。这里要注意一件事,我没有安装第一个链接的第 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);
    }

回答by HeadJ.E.M.

UPDATE

更新

This seems to be working fine on windows 10

这似乎在 Windows 10 上运行良好

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

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

you will need to add these nugets

你需要添加这些 nugets

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

回答by Perfection

I managed to gain access to the working API for windows 8 and 10 by referencing

通过引用,我设法访问了适用于 Windows 8 和 10 的工作 API

  • Windows.winmd: C:\Program?Files?(x86)\Windows?Kits\8.0\References\CommonConfiguration\Neutral
  • Windows.winmd:C:\Program?Files?(x86)\Windows?Kits\8.0\References\CommonConfiguration\Neutral

This exposes Windows.UI.Notifications.

这暴露了Windows.UI.Notifications.

回答by k94ll13nn3

You can have a look at this post for creating a COM server that is needed in order to have notifications persisted in the AC with Win32 apps https://blogs.msdn.microsoft.com/tiles_and_toasts/2015/10/16/quickstart-handling-toast-activations-from-win32-apps-in-windows-10/.

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

A working sample can be found at https://github.com/WindowsNotifications/desktop-toasts

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

回答by Ваньо Ванев

Add reference to:

添加参考:

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

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

And

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

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

And use the following code:

并使用以下代码:

 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);

ToastNotificationManager.CreateToastNotifier("Toast Sample").Show(toast);

The original code can be found here: https://www.michaelcrump.net/pop-toast-notification-in-wpf/

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