using System;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
namespace XHWK.WKTool
{
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
///
/// 录屏等待 CountdownWindow.xaml 的交互逻辑
///
public partial class CountdownWindow
{
private readonly List _images = new List();
public CountdownWindow()
{
InitializeComponent();
foreach (var imgName in _imgNames)
{
_images.Add(new BitmapImage(new Uri("pack://application:,,/Images/" + imgName)));
}
}
///
/// 计时器
///
private int _imgNum = 22;
public void InitAndShow(bool showlblShortcut = false, int changeTime = 3000)
{
imgLoding.Source = _images.Last();
Init(showlblShortcut, changeTime);
Show();
}
private System.Timers.Timer _timer;
private async void Init(bool showlblShortcut, int changeTime)
{
if (showlblShortcut)
{
lblShortcut.Visibility = Visibility.Visible;
borShortcut.Visibility = Visibility.Visible;
}
else
{
lblShortcut.Visibility = Visibility.Hidden;
borShortcut.Visibility = Visibility.Hidden;
}
_imgNum = 22;
_timer = new System.Timers.Timer();
_timer.Enabled = true;
_timer.Interval = 1.0 * changeTime / 22;
_timer.Elapsed += Timer_Elapsed;
_timer.Disposed += _timer_Disposed;
_timer.Start();
await Task.Delay(300);
}
private void _timer_Disposed(object sender, EventArgs e)
{
Dispatcher.Invoke
(
async () =>
{
// 执行操作
imgLoding.Source = null;
await Task.Delay(300);
Hide();
},
DispatcherPriority.Send
);
}
private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
if (_imgNum >= 0)
{
Dispatcher.Invoke
(
() =>
{
// 执行操作
imgLoding.Source = _images[_imgNum];
},
DispatcherPriority.Send
);
_imgNum--;
}
else
{
_timer.Stop();
_timer.Dispose();
}
}
readonly string[] _imgNames =
{
"countdown1_7.png",
"countdown1_6.png",
"countdown1_5.png",
"countdown1_4.png",
"countdown1_3.png",
"countdown1_2.png",
"countdown1_1.png",
"countdown2_7.png",
"countdown2_6.png",
"countdown2_5.png",
"countdown2_4.png",
"countdown2_3.png",
"countdown2_2.png",
"countdown2_1.png",
"countdown3_8.png",
"countdown3_7.png",
"countdown3_6.png",
"countdown3_5.png",
"countdown3_4.png",
"countdown3_3.png",
"countdown3_2.png",
"countdown3_1.png",
"countdown3_1.png",
};
}
}