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 { /// /// BubbleWindow.xaml 的交互逻辑 /// public partial class BubbleWindow : Window { public BubbleWindow() { InitializeComponent(); this.Loaded += (s, e) => this.XPopup.IsOpen = true; } /// /// “失去焦点关闭”功能使用的辅助Popup定位位置 /// 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)); /// /// 信息内容 /// 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("")); /// /// 关闭延时 /// 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)); /// /// 背景颜色 /// 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))); /// /// 前景颜色 /// 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(); /// /// 计时器到时检查是否需要关闭提示 /// 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(); } } /// /// Popup打开时开启计时器 /// 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; } /// /// 显示一个气泡提示 /// /// 提示信息:可以是文本,也可以是所有可视化元素 /// 焦点丢失关闭功能的辅助器定位位置 /// 自动关闭延时 /// 是否自动关闭 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(); } } }