WPF 右下角弹窗的简单实现
Posted dotNET跨平台
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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);
}
}
}
完啦
以上是关于WPF 右下角弹窗的简单实现的主要内容,如果未能解决你的问题,请参考以下文章