自己寫的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(); } } }
直接用的話會運行不起來,主要看后台動畫代碼和邏輯
本文屬原創 轉載請寫明出處
