WPF动态创建消息弹窗,2秒自动消失
Posted 棉晗榜
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WPF动态创建消息弹窗,2秒自动消失相关的知识,希望对你有一定的参考价值。
样式代码,放入App.xaml中
App.xaml
<Application x:Class="WpfSnqkGasAnalysis.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfSnqkGasAnalysis"
StartupUri="Login.xaml">
<Application.Resources>
<ResourceDictionary Source="Dictionary1.xaml" />
</Application.Resources>
</Application>
Dictionary1.xaml,消息框样式
<!--消息提示框-->
<Style x:Key="TextBlock_Alert" TargetType="TextBlock">
<Setter Property="Background" Value="Lime"></Setter>
<Setter Property="Padding" Value="20,10"></Setter>
<Setter Property="FontSize" Value="18"></Setter>
<Setter Property="Margin" Value="4"></Setter>
<Setter Property="HorizontalAlignment" Value="Center"></Setter>
<Setter Property="VerticalAlignment" Value="Top"></Setter>
<Setter Property="TextWrapping" Value="Wrap"></Setter>
<!--<Setter Property="Panel.ZIndex" Value="3000"></Setter>-->
</Style>
后端代码
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.Controls.Primitives;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Threading;
namespace WpfSnqkGasAnalysis
/// <summary>
/// 消息框封装,消息2秒后消失
/// </summary>
/// 创建时间:2022年11月30日15:08:25。
public static class ShowAlertHelpter
//上一次显示的top位置
static double lastTop = 0;
static double lastLeft = 0;
//上一次显示时间
static DateTime lastShowTime = DateTime.Now;
/// <summary>
/// 提示消息。消息2秒后消失
/// </summary>
/// <param name="msg">消息</param>
/// <param name="alwaysShow">是否一直显示</param>
public static void ShowAlert(string msg, bool alwaysShow = false)
//Grid grid = Application.Current.Windows[0].FindName("mainGrid") as Grid;
Grid grid = Application.Current.Windows[0].Content as Grid;
if (grid == null)
return;
//Grid grid = null;
//if (page is Window)
//
// Window window = (Window)page;
// grid = window.Content as Grid;
//
//else if (page is Page)
//
// Page args = (Page)page;
// grid = args.Content as Grid;
//
//else if (page is Grid)
//
// grid = page as Grid;
//
Application.Current.Dispatcher.BeginInvoke(
() =>
//Popup使消息在最前面显示
Popup popup = new Popup();
popup.PopupAnimation = PopupAnimation.Fade;//渐入
popup.AllowsTransparency = true;//设置Popup透明
popup.Margin = new Thickness(10);
popup.VerticalOffset = 200;
popup.HorizontalOffset = 600;
popup.HorizontalAlignment = HorizontalAlignment.Center;
//popup.IsOpen = false;
popup.IsOpen = true;
popup.ToolTip = "点击关闭";
TextBlock textBlock = new TextBlock();
textBlock.Text = msg;
textBlock.Style = (System.Windows.Style)grid.FindResource("TextBlock_Alert");
//边框阴影
System.Windows.Media.Effects.DropShadowEffect effect = new System.Windows.Media.Effects.DropShadowEffect();
effect.BlurRadius = 4;
effect.Color = Color.FromRgb(196, 196, 196);
textBlock.Effect = effect;
if (lastTop > 0 && (DateTime.Now - lastShowTime).TotalSeconds < 3)
//字符多,增加偏移
double space = 50d;
if (msg.Length > 22)
space = 70d;
popup.HorizontalOffset = lastLeft + space;
popup.VerticalOffset = lastTop + space;
//一直显示消息
if (alwaysShow || msg.Contains("失败") || msg.Contains("异常") || msg.Contains("出错"))
textBlock.Background = Brushes.Orchid;//红色背景
goto SKIP_END;
//消息提示自动消失
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromMilliseconds(2000);
timer.Tick += (s, e) =>
grid.Children.Remove(popup);
timer.Stop();
if (lastTop > 500)
lastTop = 0;
lastLeft = -200;
;
timer.Start();
SKIP_END:
lastTop = popup.VerticalOffset;
lastLeft = popup.HorizontalOffset;
lastShowTime = DateTime.Now;
popup.Child = textBlock;
grid.Children.Add(popup);
//点击就移除
popup.MouseLeftButtonUp += (s, e) =>
grid.Children.Remove(popup);
;
);
调用显示消息
private void btnSearch_Click(object sender, RoutedEventArgs e)
ShowAlertHelpter.ShowAlert("操作成功");
以上是关于WPF动态创建消息弹窗,2秒自动消失的主要内容,如果未能解决你的问题,请参考以下文章