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 NAudio.Wave;
namespace FLY.ControlLibrary
{
///
/// Window_WarningTip.xaml 的交互逻辑
///
public partial class Window_WarningTip : Window, INotifyPropertyChanged
{
static Window_WarningTip Current = null;
static Action StopEvent = null;
static Action OKEvent = null;
static int WarningTipID = 0;
private WaveOutEvent outputDevice;
private AudioFileReader audioFile;
private bool isRunning;
System.Windows.Threading.DispatcherTimer timer;
///
/// 持续时间
///
public TimeSpan Duration = TimeSpan.MinValue;
///
/// 警告信息
///
public string Message { get; set; } = "警告";
///
/// 确认键 显示内容
///
public string OKContent { get; set; } = "确定";
public Window_WarningTip()
{
InitializeComponent();
this.DataContext = this;
timer = new System.Windows.Threading.DispatcherTimer();
timer.Tick += new EventHandler(timer1_Tick);
}
void timer1_Tick(object sender, EventArgs e)
{
timer.Stop();
//到时间了,把音乐关闭
stopMusic();
//把界面关闭
Close();
Current = null;
//触发事件
StopEvent?.Invoke();
}
///
/// 关闭
///
public static void Stop()
{
Current?.timer1_Tick(null,null);
}
///
/// 关闭
///
///
public static void Stop(int id)
{
if (WarningTipID == id)
Stop();
}
void stopMusic()
{
isRunning = false;
outputDevice?.Stop();
outputDevice?.Dispose();
outputDevice = null;
audioFile?.Dispose();
audioFile = null;
}
void playMusic(string musicpath)
{
try
{
audioFile = new AudioFileReader(musicpath);
}
catch
{
//音乐无法启动
return;
}
outputDevice = new WaveOutEvent();
outputDevice.Init(audioFile);
outputDevice.PlaybackStopped += (s, e) =>
{
if (isRunning)
{
//重新播放!!
if (audioFile == null)
{
stopMusic();
return;
}
audioFile.Position = 0;
outputDevice.Play();
}
};
outputDevice.Play();
}
void Play(string musicpath)
{
if (musicpath == null)
{
//没有音乐,关闭
stopMusic();
}
else if (audioFile == null)
{
playMusic(musicpath);
}
else if (audioFile.FileName != musicpath)
{
stopMusic();
playMusic(musicpath);
}
isRunning = true;
if ((Duration == TimeSpan.MaxValue) || (Duration == TimeSpan.MinValue))
{
//永远打开
timer.Stop();
}
else
{
timer.Interval = Duration;
timer.Start();
}
}
///
/// 显示报警提示
///
/// 标题
/// 内容
/// ID
public static int Show(string title, string message)
{
return Show(title, message, TimeSpan.MaxValue);
}
///
/// 显示报警提示
///
/// 标题
/// 内容
/// 显示的时间
///
public static int Show(string title, string message, TimeSpan duration)
{
return Show(title, message, duration, null);
}
///
/// 显示报警提示
///
/// 标题
/// 内容
/// 显示的时间
/// 音乐路径
/// ID
public static int Show(string title, string message, TimeSpan duration, string musicpath)
{
return Show(title, message, duration, musicpath, null);
}
///
/// 显示报警提示
///
/// 标题
/// 内容
/// 显示的时间
/// 音乐路径
/// 关闭事件 回调函数
/// ID
public static int Show(string title, string message, TimeSpan duration, string musicpath, Action stophandler)
{
return Show(title, message, duration, musicpath, stophandler, "确定", null);
}
///
/// 显示报警提示
///
/// 标题
/// 内容
/// 显示的时间
/// 音乐路径
/// 关闭事件 回调函数
/// 确定按键 显示字符串
/// 确定事件 回调函数
/// ID
public static int Show(string title, string message, TimeSpan duration, string musicpath, Action stophandler, string okcontent, Action okhandler)
{
if (Current == null)
{
Current = new Window_WarningTip();
}
Current.Title = title;
Current.Duration = duration;
Current.Message = message;
Current.OKContent = okcontent;
StopEvent = stophandler;
OKEvent = okhandler;
Current.Owner = Application.Current.MainWindow;
Current.Show();
Current.Play(musicpath);
WarningTipID++;
if (WarningTipID >= int.MaxValue-1)
WarningTipID = 0;
return WarningTipID;
}
private void button_ok_Click(object sender, RoutedEventArgs e)
{
Stop();
OKEvent?.Invoke();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
this.Width = SystemParameters.PrimaryScreenWidth;
this.Left = 0;
this.Top = (SystemParameters.PrimaryScreenHeight - this.Height) / 2;
}
#region INotifyPropertyChanged 成员
public event PropertyChangedEventHandler PropertyChanged;
#endregion
}
}