WPF 右下角弹窗的简单实现

软件中经常出现右下角弹窗,从下面缓缓弹出的,这次就做个简陋的实现,

思路就是在窗口加载和关闭时执行动画DoubleAnimation

今天懒得做界面了,只实现了功能。

看看效果:

下面看看代码:

主窗口添加一个按钮 ,点击事件:

 private void Button_Click(object sender, RoutedEventArgs e)
        {
            NotifyWindow notifyWindow = new NotifyWindow() { Message="MessageBox" };
            notifyWindow.Show();
        }

,新建一个NotifyWindow:

xaml:

<Window x:Class="WPFDemos.NotifyWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WPFDemos"
        mc:Ignorable="d"
        Background="Transparent"
        AllowsTransparency="True"
        WindowStyle="None"
        x:Name="window"
        Title="NotifyWindow" Height="200" Width="300">
    <Grid Margin="8" Background="White">
        <Grid.Effect>
            <DropShadowEffect BlurRadius="8" ShadowDepth="0" Color="Black"/>
        </Grid.Effect>
        <Button Content="关闭" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="5" Click="Button_Click"/>
        <TextBlock Text="{Binding Message,ElementName=window}" FontSize="35" HorizontalAlignment="Center" VerticalAlignment="Center"/>
    </Grid>
</Window>


后台代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;


namespace WPFDemos
{
    /// <summary>
    /// NotifyWindow.xaml 的交互逻辑
    /// </summary>
    public partial class NotifyWindow : Window
    {
        public NotifyWindow()
        {
            InitializeComponent();
            Loaded += NotifyWindow_Loaded;
        }


        private void NotifyWindow_Loaded(object sender, RoutedEventArgs e)
        {
            Left = SystemParameters.WorkArea.Right - this.Width;
            Top = SystemParameters.WorkArea.Bottom;
            var animation = new DoubleAnimation
            {
                Duration = new Duration(TimeSpan.FromSeconds(0.5)),
                To = SystemParameters.WorkArea.Bottom - this.Height,
            };
            this.BeginAnimation(TopProperty, animation);
        }


        private string message = "Message";


        public string Message
        {
            get { return message; }
            set { message = value; }
        }


        private void Button_Click(object sender, RoutedEventArgs e)
        {


            var animation = new DoubleAnimation
            {
                Duration = new Duration(TimeSpan.FromSeconds(0.3)),
                To = SystemParameters.WorkArea.Bottom,
            };
            animation.Completed += (ss, ee) =>
            {
                this.Close();
            };
            this.BeginAnimation(TopProperty, animation);
        }
    }
}


完啦 

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值