故乡的亲情 发表于 2024-2-4 09:31:56

3.Popup 消息弹出框

 Popup 是一个用于显示临时性内容的控件,它可以在应用程序的其他内容之上显示一个弹出窗口。它通常用于实现下拉菜单、工具提示、通知消息等功能。
主要属性为:
Child:获取或设置 Popup控件的内容。
IsOpen:获取或设置一个值,该值指示Popup 是否可见
Placement:获取或设置 Popup 控件打开时的控件方向,并指定Popup 控件在与屏幕边界重叠时的控件行为
PlacementTarget:获取或设置当打开 Popup 控件时该控件相对于其放置的元素。
PopupAnimation:获取或设置Popup 控件的打开和关闭动画。
StaysOpen:获取或设置一个值,该值指示当 Popup 控件焦点不再对准时,是否关闭该控件。
 
案例:
PopupBox.xaml
<<strong>Popup</strong> x:Class="WPF.Common.Dialog.PopupBox"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:local="clr-namespace:WPF.Common.Dialog"
             mc:Ignorable="d"
             d:DesignHeight="450" d:DesignWidth="800">
    <Grid>
      <Border x:Name="border" Background="Gray" CornerRadius="8" MinHeight="35" Width="400" Height="100">
            <TextBlock TextWrapping="WrapWithOverflow"
                     Foreground="White"
                     Margin="10"
                     FontSize="18"
                     HorizontalAlignment="Center"
                     VerticalAlignment="Center"
                     Text="{Binding Path=Message,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=local:PopupBox},UpdateSourceTrigger=PropertyChanged}"/>
      </Border>
    </Grid>
</Popup>PopupBox.xaml.cs
/// <summary>
    /// PopupBox.xaml 的交互逻辑
    /// </summary>
    public partial class PopupBox : <strong>Popup</strong>
    {
      private static PopupBox dialog = new PopupBox();
      private static DispatcherTimer timer = new DispatcherTimer();

      public PopupBox()
      {
            InitializeComponent();
      }

      /// <summary>
      /// 弹出对话框
      /// </summary>
      /// <param name="message">消息</param>
      /// <param name="owner">所属窗体对象</param>
      /// <param name="seconds">对话框隐藏倒计时(秒)</param>
      public static void Show(string message, Window owner = null, int seconds = 1)
      {
            try
            {
                Application.Current.Dispatcher.Invoke(new Action(() =>
                {
                  if (owner == null)
                  {
                        owner = Application.Current.MainWindow;
                  }

                  dialog.Message = message;
                  dialog.PlacementTarget = owner;
                  dialog.Placement = PlacementMode.Center;
                  dialog.StaysOpen = true;
                  dialog.AllowsTransparency = true;
                  dialog.VerticalOffset = owner.ActualHeight / 3;
                  dialog.Opacity = 0.9;
                  dialog.IsOpen = true;

                  timer.Tick -= Timer_Tick;
                  timer.Tick += Timer_Tick;
                  timer.Interval = new TimeSpan(0, 0, seconds);
                  timer.Start();

                }));
            }
            catch
            {
            }
      }

      private static void Timer_Tick(object sender, EventArgs e)
      {
            timer.Stop();
            Task.Run(() =>
            {
                try
                {
                  for (int i = 0; i < 100; i++)
                  {
                        Thread.Sleep(5);
                        if (Application.Current == null) return;
                        Application.Current.Dispatcher.Invoke(() =>
                        {
                            dialog.Opacity -= 0.01;
                        });
                  }

                  Application.Current.Dispatcher.Invoke(() =>
                  {
                        dialog.IsOpen = false;
                        dialog.Message = string.Empty;
                  });
                }
                catch
                {

                }
            });
      }



      /// <summary>
      /// 依赖属性--透明度
      /// </summary>
      public new double Opacity
      {
            get { return (double)GetValue(OpacityProperty); }
            set { SetValue(OpacityProperty, value); }
      }

      public static new readonly DependencyProperty OpacityProperty =
            DependencyProperty.Register("Opacity", typeof(double), typeof(PopupBox), new PropertyMetadata(1.0, new PropertyChangedCallback(OnOpacityPropertyChangedCallback)));

      private static void OnOpacityPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
      {
            if (!(d is PopupBox box)) return;
            if (!(e.NewValue is double v)) return;
            box.border.Opacity = v;//赋值新的透明度
      }


      /// <summary>
      /// 依赖属性--消息
      /// </summary>
      public string Message
      {
            get { return (string)GetValue(MessageProperty); }
            set { SetValue(MessageProperty, value); }
      }

      public static readonly DependencyProperty MessageProperty =
            DependencyProperty.Register("Message", typeof(string), typeof(PopupBox), new PropertyMetadata(string.Empty));





    } 

来源:https://www.cnblogs.com/MingQiu/p/18005673
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: 3.Popup 消息弹出框