using System; using System.Threading; using System.Windows; using System.Windows.Controls; using System.Windows.Media.Imaging; using System.Windows.Threading; namespace XHWK.WKTool { /// /// CountdownWindow.xaml 的交互逻辑 /// public partial class CountdownWindow : Window { /// /// 计时用 /// private TimeSpan _timeSpan = new TimeSpan(0, 0, 0, 0, 0); DispatcherTimer timer = null; /// /// 状态 /// private State _state = State.End; /// /// 计时器状态 /// private enum State { Start, Pause, End } public CountdownWindow() { InitializeComponent(); Initialize(); } public void Initialize() { //Thread.Sleep(1200); //this.Hide(); //MediaElement.Position //((MediaElement)((object)ImgGif)).Position= TimeSpan.Zero; //timer = new DispatcherTimer //{ // Interval = TimeSpan.FromMilliseconds(2500) //}; //timer.Tick += Timer_Tick; //timer.Start(); if (timer == null) { timer = new DispatcherTimer(); timer.Tick += OnTimer; timer.Interval = new TimeSpan(0, 0, 0, 1); timer.IsEnabled = true; timer.Start(); } timer.Interval = new TimeSpan(0, 0, 0, 1); Stack(); // 开启线程 Thread tr = new Thread(new ThreadStart(Countdown)) { IsBackground = true }; tr.Start(); } private void Countdown() { while (_timeSpan.Seconds <= 2) { if (_timeSpan.Seconds > 1) { Dispatcher.Invoke( DispatcherPriority.Normal, new Action(() => { imgLoding.Source = new BitmapImage(new Uri("pack://application:,,/Images/countdown_1.png")); }) ); } else if(_timeSpan.Seconds > 0) { Dispatcher.Invoke( DispatcherPriority.Normal, new Action(() => { imgLoding.Source = new BitmapImage(new Uri("pack://application:,,/Images/countdown_2.png")); }) ); } Thread.Sleep(888); } _state = State.End; Dispatcher.Invoke( DispatcherPriority.Normal, new Action(() => { this.Hide(); }) ); } /// /// 结束 /// /// /// private void End() { _state = State.End; } /// /// 时钟回调 /// /// /// private void OnTimer(object sender, EventArgs e) { switch (_state) { case State.Start: { _timeSpan += new TimeSpan(0, 0, 0, 1); } break; case State.Pause: { } break; case State.End: { _timeSpan = new TimeSpan(); //_timeSpan = new TimeSpan(0, 23, 12, 45, 54); } break; } } /// /// 开始 /// /// /// private void Stack() { _state = State.Start; } private void Timer_Tick(object sender, EventArgs e) { this.Hide(); timer.Stop(); } /// /// 跳转页面 /// /// /// private void Window_ContentRendered(object sender, EventArgs e) { //Thread.Sleep(800); //imgLoding.Source = new BitmapImage(new Uri("pack://application:,,/Images/countdown_2.png")); //Thread.Sleep(800); //imgLoding.Source = new BitmapImage(new Uri("pack://application:,,/Images/countdown_1.png")); //Thread.Sleep(300); //this.Hide(); //Initialize(); } private void Image_MediaEnded(object sender, RoutedEventArgs e) { ((MediaElement)sender).Position = ((MediaElement)sender).Position.Add(TimeSpan.FromMilliseconds(10000)); } } }