WPF native windows 10 toasts

后端 未结 6 1489
别跟我提以往
别跟我提以往 2021-02-05 14:13

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

6条回答
  •  眼角桃花
    2021-02-05 14:22

    Add reference to:

    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

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

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

提交回复
热议问题