Window_Tip.xaml.cs 5.23 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
1 2 3 4 5 6 7 8 9 10 11 12 13
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;
using System.ComponentModel;
14
using GalaSoft.MvvmLight.Command;
潘栩锋's avatar
潘栩锋 committed
15 16 17 18 19 20 21 22

namespace FLY.ControlLibrary
{
    /// <summary>
    /// Window_Tip.xaml 的交互逻辑
    /// </summary>
    public partial class Window_Tip : Window
    {
23
        WdTipVm viewModel;
24 25 26
        /// <summary>
        /// 
        /// </summary>
潘栩锋's avatar
潘栩锋 committed
27 28 29 30
        public Window_Tip()
        {
            InitializeComponent();
        }
31 32 33 34 35 36
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="title"></param>
        /// <param name="message"></param>
        /// <param name="duration"></param>
37
        public void Init(string title, string message, TimeSpan duration)
潘栩锋's avatar
潘栩锋 committed
38
        {
39 40 41 42 43 44 45
            if (viewModel == null)
            {
                viewModel = new WdTipVm();
                DataContext = viewModel;
            }
            viewModel.Init(title, message, duration);
            
潘栩锋's avatar
潘栩锋 committed
46 47 48 49 50 51 52 53 54
        }
        /// <summary>
        /// 右下角 提示
        /// </summary>
        /// <param name="title">标题</param>
        /// <param name="message">内容</param>
        /// <param name="duration">持续时间</param>
        public static void Show(string title, string message, TimeSpan duration)
        {
55
            WdTipVm.Show(title, message, duration);
潘栩锋's avatar
潘栩锋 committed
56
        }
57 58 59 60 61 62
        /// <summary>
        /// 右下角 短时间提示
        /// </summary>
        /// <param name="title">标题</param>
        /// <param name="message">内容</param>
        public static void ShowShortTime(string title, string message)
潘栩锋's avatar
潘栩锋 committed
63
        {
64
            Show(title, message, TimeSpan.FromSeconds(2));
潘栩锋's avatar
潘栩锋 committed
65
        }
66 67 68 69 70 71 72 73 74 75 76 77
        /// <summary>
        /// 右下角 永远显示提示
        /// </summary>
        /// <param name="title">标题</param>
        /// <param name="message">内容</param>
        public static void Show(string title, string message)
        {
            Show(title, message, TimeSpan.MaxValue);

        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
潘栩锋's avatar
潘栩锋 committed
78
        {
79
            viewModel.LoadedCmd.Execute(null);
潘栩锋's avatar
潘栩锋 committed
80
        }
81
    }
82
    class WdTipVm:INotifyPropertyChanged
83 84 85
    {
        private static Window_Tip Current=null;

86 87 88
        /// <summary>
        /// 持续时间
        /// </summary>
89
        public TimeSpan Duration { get; set; }
90 91 92
        /// <summary>
        /// 提示内容
        /// </summary>
93
        public string Message { get; set; }
94 95 96
        /// <summary>
        /// 标题
        /// </summary>
97
        public string Title { get; set; }
98 99 100
        /// <summary>
        /// 加载事件
        /// </summary>
101 102

        public RelayCommand LoadedCmd { get; private set; }
103 104 105
        /// <summary>
        /// 关闭事件
        /// </summary>
106 107
        public RelayCommand CloseCmd { get; private set; }
        public event PropertyChangedEventHandler PropertyChanged;
潘栩锋's avatar
潘栩锋 committed
108 109 110


        System.Windows.Threading.DispatcherTimer timer;
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180

        public WdTipVm()
        {
            timer = new System.Windows.Threading.DispatcherTimer();
            timer.Tick += new EventHandler(timer_Tick);
            InitCmd();
        }
        public void Init(string title, string message, TimeSpan duration)
        {
            Title = title;
            Message = message;
            Duration = duration;

            if (!Current.IsVisible)
                Current.Show();

            if (duration < TimeSpan.MaxValue && duration > TimeSpan.MinValue)
            {
                timer.Interval = duration;
                timer.Start();
            }
            else
            {
                if (timer.IsEnabled)
                    timer.Stop();
            }
            
        }
        void InitCmd()
        {
            LoadedCmd = new RelayCommand(() =>
            {
                if (Current == null)
                    return;//不正常
                Current.Left = SystemParameters.PrimaryScreenWidth - Current.Width;
                Current.Top = SystemParameters.PrimaryScreenHeight - Current.Height - 100;
                if (Duration < TimeSpan.MaxValue && Duration > TimeSpan.MinValue)
                {
                    timer.Interval = Duration;
                    timer.Start();
                }
            });

            CloseCmd = new RelayCommand(() =>
            {
                if (Current == null)
                    return;//不正常
                timer_Tick(null, null);
            });
        }
        void timer_Tick(object sender, EventArgs e)
        {
            timer.Stop();
            Current.Close();
            Current = null;
        }

        /// <summary>
        /// 右下角 提示
        /// </summary>
        /// <param name="title">标题</param>
        /// <param name="message">内容</param>
        /// <param name="duration">持续时间</param>
        public static void Show(string title, string message, TimeSpan duration)
        {
            if (Current == null)
                Current = new Window_Tip();
             
            Current.Init(title, message, duration);
        }
潘栩锋's avatar
潘栩锋 committed
181 182
    }
}