WPF之实现NotifyBox
Name="ThisWindow"
WindowStyle="None"
ResizeMode="NoResize"
AllowsTransparency="True"
Background="Transparent"
Title="NotifyBox" Height="80" Width="380" Loaded="ThisWindow_Loaded"
动画
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/ModuleView;component/SummaryStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
<!--展开的动画-->
<Storyboard x:Key="OpenStoryboard" Completed="Storyboard_Completed">
<DoubleAnimation
From="0" To="{Binding ElementName=ThisWindow,Path=Width}"
Duration="00:00:00.5"
Storyboard.TargetName="RootBorder" Storyboard.TargetProperty="Width">
<DoubleAnimation.EasingFunction>
<CubicEase EasingMode="EaseOut"/>
</DoubleAnimation.EasingFunction>
</DoubleAnimation>
</Storyboard>
<!--收缩的动画-->
<Storyboard x:Key="CloseStoryboard" Completed="Storyboard_Completed_1">
<DoubleAnimation
From="{Binding ElementName=ThisWindow,Path=Width}" To="0"
Duration="00:00:00.3"
Storyboard.TargetName="RootBorder" Storyboard.TargetProperty="Width">
<DoubleAnimation.EasingFunction>
<CubicEase EasingMode="EaseOut"/>
</DoubleAnimation.EasingFunction>
</DoubleAnimation>
</Storyboard>
</ResourceDictionary>
</Window.Resources>
触发器
<Window.Triggers>
<EventTrigger RoutedEvent="Loaded">
<BeginStoryboard>
<StaticResource ResourceKey="OpenStoryboard"/>
</BeginStoryboard>
</EventTrigger>
</Window.Triggers>
边框样式
<Border HorizontalAlignment="Right" Name="RootBorder" Style="{StaticResource borderStyle}">
<TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Foreground="White" FontSize="14" Name="MessageBox" TextWrapping="Wrap"/>
</Border>
显示文本事件
public string NotifyMessage { get; set; }
private void ThisWindow_Loaded(object sender, RoutedEventArgs e)
{
MessageBox.Text = NotifyMessage;
}
显示位置右下角,展开和收缩动画事件
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 ModuleView._04NotifyBox
{
/// <summary>
/// NotifyBox.xaml 的交互逻辑
/// </summary>
public partial class NotifyBox : Window
{
public NotifyBox()
{
InitializeComponent();
// 显示位置(右下角)
Left = SystemParameters.WorkArea.Width - Width;
Top = SystemParameters.WorkArea.Height - Height - 25;
}
/// <summary>
/// 展开的动画
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
async private void Storyboard_Completed(object sender, EventArgs e)
{
await Task.Delay(1500);
BeginStoryboard(FindResource("CloseStoryboard") as Storyboard);
}
/// <summary>
/// 收缩的动画
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Storyboard_Completed_1(object sender, EventArgs e)
{
Close();
//Hide();
}
/// <summary>
/// 提示消息
/// </summary>
public string NotifyMessage { get; set; }
private void ThisWindow_Loaded(object sender, RoutedEventArgs e)
{
MessageBox.Text = NotifyMessage;
}
}
}
使用
//消息提示窗体
NotifyBox notify = new NotifyBox() { NotifyMessage = "创建解决方案" };
notify.ShowInTaskbar = false;
notify.Show();