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;
using GalaSoft.MvvmLight.Command;
namespace FLY.ControlLibrary
{
///
/// Window_Tip.xaml 的交互逻辑
///
public partial class Window_Tip : Window
{
WdTipVm viewModel;
///
///
///
public Window_Tip()
{
InitializeComponent();
}
///
/// 初始化
///
///
///
///
public void Init(string title, string message, TimeSpan duration)
{
if (viewModel == null)
{
viewModel = new WdTipVm();
DataContext = viewModel;
}
viewModel.Init(title, message, duration);
}
///
/// 右下角 提示
///
/// 标题
/// 内容
/// 持续时间
public static void Show(string title, string message, TimeSpan duration)
{
WdTipVm.Show(title, message, duration);
}
///
/// 右下角 短时间提示
///
/// 标题
/// 内容
public static void ShowShortTime(string title, string message)
{
Show(title, message, TimeSpan.FromSeconds(2));
}
///
/// 右下角 永远显示提示
///
/// 标题
/// 内容
public static void Show(string title, string message)
{
Show(title, message, TimeSpan.MaxValue);
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
viewModel.LoadedCmd.Execute(null);
}
}
class WdTipVm:INotifyPropertyChanged
{
private static Window_Tip Current=null;
///
/// 持续时间
///
public TimeSpan Duration { get; set; }
///
/// 提示内容
///
public string Message { get; set; }
///
/// 标题
///
public string Title { get; set; }
///
/// 加载事件
///
public RelayCommand LoadedCmd { get; private set; }
///
/// 关闭事件
///
public RelayCommand CloseCmd { get; private set; }
public event PropertyChangedEventHandler PropertyChanged;
System.Windows.Threading.DispatcherTimer timer;
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;
}
///
/// 右下角 提示
///
/// 标题
/// 内容
/// 持续时间
public static void Show(string title, string message, TimeSpan duration)
{
if (Current == null)
Current = new Window_Tip();
Current.Init(title, message, duration);
}
}
}