Create popup “toaster” notifications in Windows with .NET

后端 未结 6 1134
清歌不尽
清歌不尽 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:54

    I went ahead and created a CodePlex site for this that includes "Toast Popups" and control "Help Balloons". These versions have more features than what's described below. https://toastspopuphelpballoon.codeplex.com.

    This was a great jumping off point for the solution that I was looking for. I've made a couple of modifications to meet my requirements:

    • I wanted to stop the animation on mouse over.
    • "Reset" animation when mouse leave.
    • Close the Window when opacity reached 0.
    • Stack the Toast (I have not solved the problem if the number of windows exceeds the screen height)
    • Call Load from my ViewModel

    Here's my XAML

    
    
    
        
            
                
                    
                    
                    
                
    
                
                    
                    
                
    
                
    
                
    
                
    
                

    The Code Behind

    public partial class NotificationWindow : Window
    {
        public NotificationWindow()
            : base()
        {
            this.InitializeComponent();
            this.Closed += this.NotificationWindowClosed;
        }
    
        public new void Show()
        {
            this.Topmost = true;
            base.Show();
    
            this.Owner = System.Windows.Application.Current.MainWindow;
            this.Closed += this.NotificationWindowClosed;
            var workingArea = Screen.PrimaryScreen.WorkingArea;
    
            this.Left = workingArea.Right - this.ActualWidth;
            double top = workingArea.Bottom - this.ActualHeight;
    
            foreach (Window window in System.Windows.Application.Current.Windows)
            {                
                string windowName = window.GetType().Name;
    
                if (windowName.Equals("NotificationWindow") && window != this)
                {
                    window.Topmost = true;
                    top = window.Top - window.ActualHeight;
                }
            }
    
            this.Top = top;
        }
        private void ImageMouseUp(object sender, 
            System.Windows.Input.MouseButtonEventArgs e)
        {
            this.Close();
        }
    
        private void DoubleAnimationCompleted(object sender, EventArgs e)
        {
            if (!this.IsMouseOver)
            {
                this.Close();
            }
        }
    }
    

    The call from the ViewModel:

        private void ShowNotificationExecute()
        {
            App.Current.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(
                () =>
                {
                    var notify = new NotificationWindow();
                    notify.Show();
                }));
        }
    

    The Styles referenced in the XAML:

         
    
        
            
            
        
    

    UPDATE: I added this event handler when the form is closed to "drop" the other windows.

        private void NotificationWindowClosed(object sender, EventArgs e)
        {
            foreach (Window window in System.Windows.Application.Current.Windows)
            {
                string windowName = window.GetType().Name;
    
                if (windowName.Equals("NotificationWindow") && window != this)
                {
                    // Adjust any windows that were above this one to drop down
                    if (window.Top < this.Top)
                    {
                        window.Top = window.Top + this.ActualHeight;
                    }
                }
            }
        }
    

提交回复
热议问题