Flash Message
2013-07-15 21:26 leavingys 阅读(294) 评论(0) 收藏 举报<Window x:Class="FlashMessageTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<LinearGradientBrush x:Key="flashMessageErrorBackground" StartPoint="0.5,0" EndPoint="0.5,1">
<GradientStop Color="#FFF2C6C6"/>
<GradientStop Color="#FFEEA2A2" Offset="1"/>
<GradientStop Color="#FFF0B6B6" Offset="0.487"/>
<GradientStop Color="#FFF1BDBD" Offset="0.228"/>
<GradientStop Color="#FFEFABAB" Offset="0.797"/>
</LinearGradientBrush>
<LinearGradientBrush x:Key="flashMessageInfoBackground" StartPoint="0.5,0" EndPoint="0.5,1">
<GradientStop Color="#FF71F96C"/>
<GradientStop Color="#FF96F696" Offset="1"/>
<GradientStop Color="#FF7BFF7C" Offset="0.487"/>
<GradientStop Color="#FF6AFF6A" Offset="0.228"/>
<GradientStop Color="#FF89FF89" Offset="0.797"/>
</LinearGradientBrush>
<LinearGradientBrush x:Key="flashMessageNoticeBackground" StartPoint="0.5,0" EndPoint="0.5,1">
<GradientStop Color="#FFFDFEAD"/>
<GradientStop Color="#FFF0F17F" Offset="1"/>
<GradientStop Color="#FFF6F797" Offset="0.487"/>
<GradientStop Color="#FFFAFBA3" Offset="0.228"/>
<GradientStop Color="#FFF3F48C" Offset="0.797"/>
</LinearGradientBrush>
</Window.Resources>
<Grid>
<Border>
<Grid Margin="0,-30,0,0" Name="flashMessage" Height="27" VerticalAlignment="Top" Background="{StaticResource flashMessageNoticeBackground}">
<TextBlock Name="messageText" Text="连接即将丢失,请保存所有操作。" VerticalAlignment="Center" Margin="10,0,0,0"
FontStyle="Italic" FontWeight="Bold" />
</Grid>
<Border.Effect>
<DropShadowEffect BlurRadius="5" Color="Black" Direction="315" ShadowDepth="1" />
</Border.Effect>
</Border>
<Button Height="24" Width="80" Content="提示" Click="Button_Click" />
</Grid>
</Window>
using System;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
namespace FlashMessageTest
{
public partial class MainWindow : Window
{
Storyboard _sbd = new Storyboard();
public MainWindow()
{
InitializeComponent();
InitializeAnimation();
}
private void InitializeAnimation()
{
ThicknessAnimation ta = new ThicknessAnimation();
ta.From = new Thickness(0, -30, 0, 0);
ta.To = new Thickness(0);
ta.Duration = new Duration(TimeSpan.FromMilliseconds(200));
ta.AutoReverse = false;
Storyboard.SetTarget(ta, flashMessage);
Storyboard.SetTargetProperty(ta, new PropertyPath(Grid.MarginProperty));
_sbd.Children.Add(ta);
}
public void FlashMessage(MessageType messageType)
{
string key = "flashMessage" + messageType + "Background";
flashMessage.Background = (Brush)FindResource(key);
ThicknessAnimation ta = (ThicknessAnimation)_sbd.Children[0];
ta.From = new Thickness(0, -30, 0, 0);
ta.To = new Thickness(0);
_sbd.Completed += this.FlashMessageCompleted;
_sbd.Begin();
}
public void FlashMessageCompleted(object sender, EventArgs e)
{
Action action = new Action(() => { Thread.Sleep(1000); });
action.BeginInvoke(new AsyncCallback((result) =>
{
this.Dispatcher.Invoke(new Action(() =>
{
ThicknessAnimation ta = (ThicknessAnimation)_sbd.Children[0];
ta.From = new Thickness(0);
ta.To = new Thickness(0, -30, 0, 0);
_sbd.Begin();
_sbd.Completed -= this.FlashMessageCompleted;
}));
}), null);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
FlashMessage(MessageType.Info);
}
}
public enum MessageType
{
Info,
Error,
Notice
}
}