自己写的wpf 弹出框,欢迎拍砖,动画都写在了后台代码,前台代码不太重要,用了一下iconfont,具体样式我就不贴出来了,本次主要是后台代码的动画
需要有父级窗口才可以使用。
前台代码:
<Window x:Class="V2VReporter.Views.SystemViews.MsgPopup" 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:V2VReporter.Views.SystemViews" xmlns:controls="clr-namespace:V2VReporter.Controls" mc:Ignorable="d" Title="系统消息" Height="230" Width="300" AllowsTransparency="True" WindowStyle="None" ShowInTaskbar="False"> <Border BorderBrush="{StaticResource SideLevel1}" BorderThickness="1"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="30"></RowDefinition> <RowDefinition Height="*"></RowDefinition> <RowDefinition Height="Auto"></RowDefinition> </Grid.RowDefinitions> <Grid Grid.Row="0" Background="{StaticResource MainGreen}"> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"></ColumnDefinition> <ColumnDefinition Width="*"></ColumnDefinition> </Grid.ColumnDefinitions> <StackPanel Grid.Column="0" Orientation="Horizontal"> <TextBlock Text="" Style="{StaticResource IconFont}" VerticalAlignment="Center" FontSize="18" Foreground="AliceBlue" Margin="5,0,0,0"></TextBlock> <TextBlock Text="系统消息" Foreground="AliceBlue" FontSize="15" VerticalAlignment="Center" Margin="2,0,0,0" ></TextBlock> </StackPanel> <StackPanel Grid.Column="1" HorizontalAlignment="Right"> <controls:IconButton Style="{StaticResource TitleCloseBtn}" Margin="0,5,2,0" Click="ButtonBase_OnClick"></controls:IconButton> </StackPanel> </Grid> </Grid> </Border> </Window>
后台代码:
using System; using System.Collections.Generic; using System.ComponentModel; 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; using V2VReporter.Resource; namespace V2VReporter.Views.SystemViews { /// <summary> /// MsgPopup.xaml 的交互逻辑 /// </summary> public partial class MsgPopup:Window { /// <summary> /// 关闭动画 /// </summary> private Storyboard sb_close=new Storyboard(); /// <summary> /// 关闭标识 /// </summary> private bool close_flg = false; /// <summary> /// 关闭动画 /// </summary> private DoubleAnimation close=new DoubleAnimation(); /// <summary> /// 窗口弹出 /// </summary> private Storyboard sb_loaded=new Storyboard(); /// <summary> /// 窗口弹出动画 /// </summary> private DoubleAnimation load=new DoubleAnimation(); /// <summary> /// 窗口加载动画 /// </summary> private DoubleAnimation loadOp=new DoubleAnimation(); public MsgPopup(Window win) { InitializeComponent(); initedCloseStory(); initedLoadStory(win); sb_loaded.Begin(); } private void initedLoadStory(Window win) { this.Owner = win; this.Left = win.Width - this.Width; load.From = win.Height; load.To = win.Height - this.Height; load.Duration = new Duration(TimeSpan.FromSeconds(1.5)); load.FillBehavior = FillBehavior.HoldEnd; load.AutoReverse = false; sb_loaded.Children.Add(load); Storyboard.SetTarget(load, this); Storyboard.SetTargetProperty(load, new PropertyPath("Top")); loadOp.From = 0; loadOp.To = 1; loadOp.Duration=new Duration(TimeSpan.FromSeconds(1.5)); loadOp.FillBehavior = FillBehavior.HoldEnd; loadOp.AutoReverse = false; sb_loaded.Children.Add(loadOp); Storyboard.SetTarget(loadOp, this); Storyboard.SetTargetProperty(loadOp, new PropertyPath("Opacity")); } private void initedCloseStory() { close.From = 1; close.To = 0; close.Duration = new Duration(TimeSpan.FromSeconds(1)); close.FillBehavior = FillBehavior.HoldEnd; close.AutoReverse = false; sb_close.Children.Add(close); Storyboard.SetTarget(close, this); Storyboard.SetTargetProperty(close, new PropertyPath("Opacity")); } protected override void OnClosing(CancelEventArgs e) { base.OnClosing(e); if (!close_flg) { sb_close.Completed += delegate { close_flg = true; this.Close(); }; sb_close.Begin(); e.Cancel = true; } else { e.Cancel = false; } } private void ButtonBase_OnClick(object sender, RoutedEventArgs e) { this.Close(); } } }
直接用的话会运行不起来,主要看后台动画代码和逻辑
本文属原创 转载请写明出处