Create popup “toaster” notifications in Windows with .NET

后端 未结 6 1140
清歌不尽
清歌不尽 2020-12-02 04:12

I am using .NET and am creating a desktop app/service that will display notifications in the corner of my Desktop when certain events are triggered. I don\'t want to use a r

6条回答
  •  庸人自扰
    2020-12-02 04:44

    I used the answer above to design my own notification window that is a bit more user friendly, in my opinion, and uses a few techniques that took me a little while to figure out. Sharing in case it helps anyone else out there.:

    1. Added MouseEnter event trigger to immediately set window opacity to 1 so a user does not have to wait for the window to fade into full view.
    2. Added MouseLeave event trigger to fade the window opacity to 0 when the user moves the mouse out of the window.
    3. Added MouseUp (mouse click) event trigger to immediately set window opacity to 0 to hide the notification window.
    4. If you have to Show() and Hide() the notification window multiple times, then below method also resets the Storyboard at the end so that the next Show() operation starts the operation from the start and there is no glitch in the animation.

    XAML:

    
    
    
        
    
            
    
                
    
                    
    
                    
    
                        
    
                        
    
                    
    
                
    
            
    
            
    
                
                    
                        
                            
                                
                                    
                                    
                                
                                
                                    
                                    
                                
                                
                                    
                                    
                                
                            
                        
                    
                
    
                
                    
                        
                        
                            
                                
                                    
                                
                            
                        
                    
                
    
                
                    
                        
                        
                            
                                
                                    
                                    
                                
                            
                        
                    
                
    
                
                    
                        
                        
                        
                        
                        
                            
                                
                                    
                                
                            
                        
                        
                        
                        
                
            
    
            
                
            
    
        
    
    
    

    Code behind:

    using System;
    using System.Windows;
    using System.Windows.Threading;
    
    public partial class ToastNotificationWindow
    {
        public ToastNotificationWindow()
        {
            InitializeComponent();
    
            Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, new Action(() =>
            {
                var workingArea = System.Windows.SystemParameters.WorkArea;
                var transform = PresentationSource.FromVisual(this).CompositionTarget.TransformFromDevice;
                var corner = transform.Transform(new Point(workingArea.Right, workingArea.Bottom));
    
                this.Left = corner.X - this.ActualWidth - 10;
                this.Top = corner.Y - this.ActualHeight;
            }));
        }
    }
    

提交回复
热议问题