我有一个倒数计时器,我想要一个警报在它到达终点时熄灭...很多适用于Android和iOS,但我找不到WP8。
我正在使用DispatcherTimer进行倒计时。
我正在寻找一些有关如何在计时器完成倒计时时调出声音的信息或示例。我点击按钮启动计数器并长按重置。这一切都很好,只是想在倒数后停止时产生声音效果。
private DateTime EndTime { get; set; }
private DispatcherTimer _dispatcherTimer;
private void BtnCounter_Click(object sender, RoutedEventArgs e)
{
if (this._dispatcherTimer == null)
{
this._dispatcherTimer = new DispatcherTimer();
this._dispatcherTimer.Interval = TimeSpan.FromMilliseconds(100);
this._dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
}
if (this.EndTime == DateTime.MinValue)
{
this.EndTime = DateTime.Now + (TimeSpan)this.tsPicker.Value;
}
this._dispatcherTimer.Start();
}
void dispatcherTimer_Tick(object sender, EventArgs e)
{
var remaining = this.EndTime - DateTime.Now;
int remainingSeconds = (int)remaining.TotalSeconds;
this.tsPicker.Value = TimeSpan.FromSeconds(remainingSeconds);
if (remaining.TotalSeconds <= 0)
{
this._dispatcherTimer.Stop();
// Sound code should go here, or a method call to it.
}
}
private void BtnCounter_Hold(object sender, System.Windows.Input.GestureEventArgs e)
{
cnt = 0;
BtnCounter.Content = cnt;
this._dispatcherTimer.Stop();
this.EndTime = DateTime.MinValue;
this.tsPicker.Value = TimeSpan.FromSeconds(0);
}
protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
{
this.EndTime = DateTime.MinValue;
base.OnNavigatedFrom(e);
}