WPF native windows 10 toasts

后端 未结 6 1374
别跟我提以往
别跟我提以往 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: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 ;)

提交回复
热议问题