WPF Flyout风格动画消息弹出消息提示框

WPF Flyout风格动画消息弹出消息提示框

效果如图:

XAML:

csharp 复制代码
<Window x:Class="你的名称控件.FlyoutNotication"
        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:dxi="http://schemas.devexpress.com/winfx/2008/xaml/core/internal"
        xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm"
        xmlns:dxt="http://schemas.devexpress.com/winfx/2008/xaml/core/themekeys"
        xmlns:local="clr-namespace:SMAT.Controls"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        Title="FlyoutNotication"
        Width="300"
        Height="80"
        AllowsTransparency="True"
        Background="Transparent"
        ResizeMode="NoResize"
        WindowStartupLocation="Manual"
        WindowStyle="None"
        mc:Ignorable="d">
    <Window.Resources>
        <Storyboard x:Key="Show">
            <DoubleAnimation Storyboard.TargetName="Translate"
                             Storyboard.TargetProperty="X"
                             From="310"
                             To="0"
                             Duration="0:0:0.5">
                <DoubleAnimation.EasingFunction>
                    <CubicEase EasingMode="EaseOut" />
                </DoubleAnimation.EasingFunction>
            </DoubleAnimation>
        </Storyboard>
        <Storyboard x:Key="Hide">
            <DoubleAnimation Storyboard.TargetName="Translate"
                             Storyboard.TargetProperty="X"
                             To="310"
                             Duration="0:0:0.5">
                <DoubleAnimation.EasingFunction>
                    <CubicEase EasingMode="EaseOut" />
                </DoubleAnimation.EasingFunction>
            </DoubleAnimation>
        </Storyboard>
    </Window.Resources>
    <Border Background="Gray" BorderBrush="LightGray" BorderThickness="1">
        <Border.RenderTransform>
            <TransformGroup>
                <TranslateTransform x:Name="Translate" X="300" />
            </TransformGroup>
        </Border.RenderTransform>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="auto" />
                <RowDefinition />
            </Grid.RowDefinitions>

            <TextBlock x:Name="Title"
                       Margin="12,12,4,4"
                       FontSize="16"
                       FontWeight="Bold"
                       Text="标题" />

            <TextBlock x:Name="Message"
                       Grid.Row="1"
                       Margin="12,4"
                       Text="消息内容"
                       TextWrapping="Wrap" />
        </Grid>
    </Border>

</Window>

C#:

csharp 复制代码
 /// <summary>
 /// FlyoutNotication.xaml 的交互逻辑
 /// </summary>
 public partial class FlyoutNotication : Window
 {
     public FlyoutNotication()
     {
         InitializeComponent();
     }

     public static async void ShowNotication(string message, string title)
     {
         FlyoutNotication flyoutNotication = new FlyoutNotication()
         {
             Owner = Application.Current.MainWindow,
             ShowInTaskbar = false,
             Left = Application.Current.MainWindow.ActualWidth+ (Application.Current.MainWindow.WindowState == WindowState.Maximized?-20: Application.Current.MainWindow.Left) - 300,
             Top = Application.Current.MainWindow.ActualHeight + (Application.Current.MainWindow.WindowState == WindowState.Maximized ? -20 : Application.Current.MainWindow.Top) - 80,
         };

         flyoutNotication.Message.Text = message;
         flyoutNotication.Title.Text = title;
         var sbShow = flyoutNotication.FindResource("Show") as Storyboard;
         flyoutNotication.Show();
         sbShow.Begin();

         await Task.Delay(2000);
         var sbHide = flyoutNotication.FindResource("Hide") as Storyboard;
         sbHide.Completed += (s, e) => flyoutNotication.Close();
         sbHide.Begin();
     }
 }

调用控件:

csharp 复制代码
FlyoutNotication.ShowNotication("测试消息提示", "标题");
相关推荐
Scout-leaf6 天前
WPF新手村教程(三)—— 路由事件
c#·wpf
柒.梧.9 天前
基于SpringBoot+JWT 实现Token登录认证与登录人信息查询
wpf
十月南城12 天前
Flink实时计算心智模型——流、窗口、水位线、状态与Checkpoint的协作
大数据·flink·wpf
听麟14 天前
HarmonyOS 6.0+ 跨端会议助手APP开发实战:多设备接续与智能纪要全流程落地
分布式·深度学习·华为·区块链·wpf·harmonyos
@hdd14 天前
Kubernetes 可观测性:Prometheus 监控、日志采集与告警
云原生·kubernetes·wpf·prometheus
zls36536515 天前
C# WPF canvas中绘制缺陷分布map
开发语言·c#·wpf
专注VB编程开发20年15 天前
c#Redis扣款锁的设计,多用户,多台电脑操作
wpf
闲人编程16 天前
定时任务与周期性调度
分布式·python·wpf·调度·cron·定时人物·周期性
zls36536516 天前
C# WPF canvas中绘制缺陷分布map并实现缩放
开发语言·c#·wpf
数据知道17 天前
PostgreSQL:Citus 分布式拓展,水平分片,支持海量数据与高并发
分布式·postgresql·wpf