我正在开发一个UWP应用程序,我的一个页面实际上是供用户拍摄它们的照片。在页面中,我有定时器供用户在拍照前选择。
但是,我希望显示一个计时器,在相机屏幕中倒计时,以便用户在拍摄照片之前知道他们准备多少时间。
关于我如何做到这一点的任何想法?谢谢!
万一需要,以下是我的定时器和拍照按钮的代码:
private async void PhotoButton_Click(object sender, RoutedEventArgs e)
{
//If preview is not running, no preview frames can be acquired
if (!_isPreviewing) return;
await Task.Delay(TimeSpan.FromSeconds(_seconds));
await TakePhotoAsync();
await GetPreviewFrameAsSoftwareBitmapAsync();
PreviewFrameBackground.Visibility = Visibility.Visible;
}
private void Timer_3sec_Click(object sender, RoutedEventArgs e)
{
Timer_5sec.Opacity = 0.2;
Timer_7sec.Opacity = 0.2;
Timer_3sec.Opacity = 1.0;
_seconds = 3;
}
private void Timer_5sec_Click(object sender, RoutedEventArgs e)
{
Timer_3sec.Opacity = 0.2;
Timer_7sec.Opacity = 0.2;
Timer_5sec.Opacity = 1.0;
_seconds = 5;
}
private void Timer_7sec_Click(object sender, RoutedEventArgs e)
{
Timer_3sec.Opacity = 0.2;
Timer_5sec.Opacity = 0.2;
Timer_7sec.Opacity = 1.0;
_seconds = 7;
}
答案 0 :(得分:1)
您可以使用DispatcherTimer来解决问题。
这里有一些代码示例如何做到这一点(示例不显示如何捕获或显示剩余的秒数,只是为了计算它们!)
类参数:
private int _startTime;
private DispatcherTimer _timer = new DispatcherTimer();
方法:
private void StartTimer()
{
_timer.Interval = TimeSpan.FromMilliseconds(500);
_timer.Tick += Timer_Tick;
_startTime = Environment.TickCount;
_timer.Start();
}
private void Timer_Tick(object sender, object e)
{
var remainingSeconds = _seconds - TimeSpan.FromMilliseconds(Environment.TickCount - _startTime).Seconds;
if(remainingSeconds <= 0)
{
_timer.Stop();
_timer.Tick -= Timer_Tick;
timerText.Text = "0 Seconds";
//Capture Image
} else
{
timerText.Text = "" + remainingSeconds + " Seconds";
}
}
在设置StartTimer
之后,您需要在Click-Methods中调用_seconds
- 方法。