WPF native windows 10 toasts

后端 未结 6 1382
别跟我提以往
别跟我提以往 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/

    0 讨论(0)
  • 2021-02-05 14:28

    You can use a NotifyIcon from System.Windows.Forms namespace like this:

    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 page for parameters of ShowBalloonTip.

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

    0 讨论(0)
  • 2021-02-05 14:36

    UPDATE

    This seems to be working fine on windows 10

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

    you will need to add these nugets

    Install-Package WindowsAPICodePack-Core
    Install-Package WindowsAPICodePack-Shell
    
    0 讨论(0)
  • 2021-02-05 14:38

    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.

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

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

    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.

    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.

    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 Details and ensure AppUserModeId is checked. Compare your AppUserModelId against other installed apps.

    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.

    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);
        }
    
    0 讨论(0)
  • 2021-02-05 14:43

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

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

    This exposes Windows.UI.Notifications.

    0 讨论(0)
  • 2021-02-05 14:44

    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/.

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

    0 讨论(0)
提交回复
热议问题