Files
Future/Future.Contract/Bubbles/BubbleWindow.xaml.cs

147 lines
5.4 KiB
C#
Raw Normal View History

2025-08-30 17:19:57 +08:00
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.Imaging;
using System.Windows.Shapes;
namespace Future.Contract
{
/// <summary>
/// BubbleWindow.xaml 的交互逻辑
/// </summary>
public partial class BubbleWindow : Window
{
public BubbleWindow()
{
InitializeComponent();
this.Loaded += (s, e) => this.XPopup.IsOpen = true;
}
/// <summary>
/// “失去焦点关闭”功能使用的辅助Popup定位位置
/// </summary>
public UIElement Placement
{
get { return (UIElement)GetValue(PlacementProperty); }
set { SetValue(PlacementProperty, value); }
}
// Using a DependencyProperty as the backing store for Element. This enables animation, styling, binding, etc...
public static readonly DependencyProperty PlacementProperty =
DependencyProperty.Register("Placement", typeof(UIElement), typeof(Window), new PropertyMetadata(null));
/// <summary>
/// 信息内容
/// </summary>
public object InfoContent
{
get { return (string)GetValue(InfoContentProperty); }
set { SetValue(InfoContentProperty, value); }
}
// Using a DependencyProperty as the backing store for InfoContent. This enables animation, styling, binding, etc...
public static readonly DependencyProperty InfoContentProperty =
DependencyProperty.Register("InfoContent", typeof(object), typeof(Window), new PropertyMetadata(""));
/// <summary>
/// 关闭延时
/// </summary>
public int Delay
{
get { return (int)GetValue(DelayProperty); }
set { SetValue(DelayProperty, value); }
}
// Using a DependencyProperty as the backing store for Delay. This enables animation, styling, binding, etc...
public static readonly DependencyProperty DelayProperty =
DependencyProperty.Register("Delay", typeof(int), typeof(Window), new PropertyMetadata(3));
/// <summary>
/// 背景颜色
/// </summary>
public Brush BackBrush
{
get { return (Brush)GetValue(BackBrushProperty); }
set { SetValue(BackBrushProperty, value); }
}
// Using a DependencyProperty as the backing store for BackBrush. This enables animation, styling, binding, etc...
public static readonly DependencyProperty BackBrushProperty =
DependencyProperty.Register("BackBrush", typeof(Brush), typeof(Window), new PropertyMetadata(new SolidColorBrush(Colors.Gray)));
/// <summary>
/// 前景颜色
/// </summary>
public Brush InfoBrush
{
get { return (Brush)GetValue(InfoBrushProperty); }
set { SetValue(InfoBrushProperty, value); }
}
// Using a DependencyProperty as the backing store for BackBrush. This enables animation, styling, binding, etc...
public static readonly DependencyProperty InfoBrushProperty =
DependencyProperty.Register("InfoBrush", typeof(Brush), typeof(Window), new PropertyMetadata(new SolidColorBrush(Colors.White)));
DateTime mStartTime = DateTime.Now;
System.Windows.Threading.DispatcherTimer mUiTimer = new System.Windows.Threading.DispatcherTimer();
/// <summary>
/// 计时器到时检查是否需要关闭提示
/// </summary>
private void UITimerTick(object sender, EventArgs e)
{
if (XPopup.IsOpen == false)
{
if (this.Delay > 0) mUiTimer.Stop();
this.Close();
}
if ((DateTime.Now - mStartTime).TotalSeconds >= Delay)
{
XPopup.IsOpen = false;
this.Close();
}
}
/// <summary>
/// Popup打开时开启计时器
/// </summary>
private void XPopup_Opened(object sender, EventArgs e)
{
if (this.Delay <= 0) return;
if (mUiTimer.IsEnabled == false)
{
mUiTimer = new System.Windows.Threading.DispatcherTimer();
mUiTimer.Interval = TimeSpan.FromMilliseconds(500);
mUiTimer.Tick += UITimerTick;
mStartTime = DateTime.Now;
mUiTimer.Start();
}
else mStartTime = DateTime.Now;
}
/// <summary>
/// 显示一个气泡提示
/// </summary>
/// <param name="infoContent">提示信息:可以是文本,也可以是所有可视化元素</param>
/// <param name="placement">焦点丢失关闭功能的辅助器定位位置</param>
/// <param name="delayTime">自动关闭延时</param>
/// <param name="autoClose">是否自动关闭</param>
public static void ShowBubble(object infoContent, FrameworkElement placement = null, int delayTime = 1, bool autoClose = true)
{
BubbleWindow window = new BubbleWindow();
window.Placement = placement;
window.InfoContent = infoContent;
window.Delay = delayTime;
window.Show();
}
}
}