如何在 UWP 中为定时器做一个循环
How to do a loop for a timer in UWP
我正在尝试在 UWP 中编写计时器。
我想让我的计时器做一个循环,例如倒计时 1 分钟,然后倒计时 2 分钟暂停,连续 5 次,而不必重新按下开始按钮。
我能够倒计时 1 分钟并进入 2 分钟暂停,但我不知道如何回到 1 分钟倒计时。循环只是卡在 2 分钟的暂停中。
我试过做一个 IF, ELSE。我试过 bool, true, false..
public sealed partial class MainPage : Page
{
public int x;
static FenetreParametres infopage = FenetreParametres.Current;
static int Task = Int32.Parse(infopage.getTmpRound());
//static int nb = Int32.Parse(infopage.getNbRound());
//static int pause = Int32.Parse(infopage.getTmpPause());
MediaPlayer player;
private int _restsTaken { get; set; }
private int _currentTime { get; set; }
private DispatcherTimer _timer { get; set; }
private TimeSpan _tickInterval { get; set; }
private TimeSpan _intervalRemainingTime { get; set; }
private DateTime _intervalEnd { get; set; }
private bool _isRunning = false;
private bool verif = false;
public MainPage()
{
this.InitializeComponent();
_currentTime = 0;
_tickInterval = TimeSpan.FromSeconds(1);
this.initializeTimer(_tickInterval.Seconds);
this.initializeDisplayTimer(0);
player = new MediaPlayer();
}
private void initializeTimer(int tickInterval)
{
_timer = new DispatcherTimer();
_timer.Interval = TimeSpan.FromSeconds(tickInterval);
_timer.Tick += interval_Tick;
}
private void initializeDisplayTimer(int intervalTime)
{
_intervalRemainingTime = TimeSpan.FromMinutes(intervalTime);
timerLabel.Text = _intervalRemainingTime.ToString();
}
private void interval_Tick(object sender, object e)
{
int previousTimeInMinutes = _intervalRemainingTime.Minutes;
_isRunning = true;
_intervalRemainingTime = _intervalRemainingTime.Subtract(_tickInterval);
timerLabel.Text = _intervalRemainingTime.ToString();
if (previousTimeInMinutes != _intervalRemainingTime.Minutes)
{
string timeIndicator = _intervalRemainingTime.Minutes == 0 ? "1 >" : _intervalRemainingTime.Minutes.ToString();
}
if (TimeSpan.Equals(_intervalRemainingTime, TimeSpan.Zero))
{
playmusic();
this.initializeDisplayTimer(2);
}
}
private async void playmusic()
{
Windows.Storage.StorageFolder folder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync(@"Assets");
Windows.Storage.StorageFile file = await folder.GetFileAsync("ding.wav");
player.AutoPlay = false;
player.Source = MediaSource.CreateFromStorageFile(file);
player.Play();
}
private void trait()
{
for (int i = 1; i <= 3; i++)
{
//timerLabel.Text = "Pause";
//x = duree;
playmusic();
_currentTime = x;
this.initializeDisplayTimer(_currentTime);
x--;
_intervalEnd = DateTime.Now.Add(_intervalRemainingTime);
_timer.Start();
}
}
private void Button_Click_Start(object sender, RoutedEventArgs e)
{
if (TimeSpan.Equals(_intervalRemainingTime, TimeSpan.Zero))
{
playmusic();
this.initializeDisplayTimer(Task);
_timer.Start();
}
}
private void Button_Click_Pause(object sender, RoutedEventArgs e)
{
_isRunning = false;
_timer.Stop();
}
private void Button_Click_Reset(object sender, RoutedEventArgs e)
{
_timer.Stop();
this.initializeDisplayTimer(_currentTime);
}
}
预计进入暂停2分钟后重新进入1分钟倒计时,连续5次
你的问题实际上是一个基本的c#代码逻辑问题。它不特定于 UWP 编程。如果你把整个逻辑理解清楚了,通过c#代码逻辑就很容易搞定你的target了。
我刚刚做了一个简单的代码示例供大家参考:
<Grid>
<Grid.Resources>
<Style TargetType="TextBlock">
<Setter Property="VerticalAlignment" Value="Center"></Setter>
</Style>
</Grid.Resources>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<TextBlock Grid.Column="0" Text="Count Down Time: "></TextBlock>
<TextBlock x:Name="countDown1" Grid.Column="1"></TextBlock>
<TextBlock Grid.Row="1" Grid.Column="0" Text="Pause Remaning: "></TextBlock>
<TextBlock x:Name="pauseRemaning" Grid.Row="1" Grid.Column="1"></TextBlock>
<TextBlock Grid.Row="2" Grid.Column="0" Text="Looping Times: "></TextBlock>
<TextBlock x:Name="loop_Times" Grid.Row="2" Grid.Column="1"></TextBlock>
</Grid>
public sealed partial class MainPage : Page
{
private int countDowm = 60;
private int RemainingTime = 120;
private int loopTimes = 0;
public MainPage()
{
this.InitializeComponent();
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(1);
timer.Tick += Timer_Tick;
timer.Start();
}
private void Timer_Tick(object sender, object e)
{
if (loopTimes == 5)
{
countDowm = 60;
RemainingTime = 120;
loopTimes = 0;
}
else
{
if (countDowm <= 0)
{
if (RemainingTime <= 0)
{
loopTimes++;
countDowm = 60;
RemainingTime = 120;
}
else
{
RemainingTime--;
}
}
else
{
countDowm--;
}
}
countDown1.Text = countDowm.ToString();
pauseRemaning.Text = RemainingTime.ToString();
loop_Times.Text = loopTimes.ToString();
}
}
我正在尝试在 UWP 中编写计时器。 我想让我的计时器做一个循环,例如倒计时 1 分钟,然后倒计时 2 分钟暂停,连续 5 次,而不必重新按下开始按钮。 我能够倒计时 1 分钟并进入 2 分钟暂停,但我不知道如何回到 1 分钟倒计时。循环只是卡在 2 分钟的暂停中。
我试过做一个 IF, ELSE。我试过 bool, true, false..
public sealed partial class MainPage : Page
{
public int x;
static FenetreParametres infopage = FenetreParametres.Current;
static int Task = Int32.Parse(infopage.getTmpRound());
//static int nb = Int32.Parse(infopage.getNbRound());
//static int pause = Int32.Parse(infopage.getTmpPause());
MediaPlayer player;
private int _restsTaken { get; set; }
private int _currentTime { get; set; }
private DispatcherTimer _timer { get; set; }
private TimeSpan _tickInterval { get; set; }
private TimeSpan _intervalRemainingTime { get; set; }
private DateTime _intervalEnd { get; set; }
private bool _isRunning = false;
private bool verif = false;
public MainPage()
{
this.InitializeComponent();
_currentTime = 0;
_tickInterval = TimeSpan.FromSeconds(1);
this.initializeTimer(_tickInterval.Seconds);
this.initializeDisplayTimer(0);
player = new MediaPlayer();
}
private void initializeTimer(int tickInterval)
{
_timer = new DispatcherTimer();
_timer.Interval = TimeSpan.FromSeconds(tickInterval);
_timer.Tick += interval_Tick;
}
private void initializeDisplayTimer(int intervalTime)
{
_intervalRemainingTime = TimeSpan.FromMinutes(intervalTime);
timerLabel.Text = _intervalRemainingTime.ToString();
}
private void interval_Tick(object sender, object e)
{
int previousTimeInMinutes = _intervalRemainingTime.Minutes;
_isRunning = true;
_intervalRemainingTime = _intervalRemainingTime.Subtract(_tickInterval);
timerLabel.Text = _intervalRemainingTime.ToString();
if (previousTimeInMinutes != _intervalRemainingTime.Minutes)
{
string timeIndicator = _intervalRemainingTime.Minutes == 0 ? "1 >" : _intervalRemainingTime.Minutes.ToString();
}
if (TimeSpan.Equals(_intervalRemainingTime, TimeSpan.Zero))
{
playmusic();
this.initializeDisplayTimer(2);
}
}
private async void playmusic()
{
Windows.Storage.StorageFolder folder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync(@"Assets");
Windows.Storage.StorageFile file = await folder.GetFileAsync("ding.wav");
player.AutoPlay = false;
player.Source = MediaSource.CreateFromStorageFile(file);
player.Play();
}
private void trait()
{
for (int i = 1; i <= 3; i++)
{
//timerLabel.Text = "Pause";
//x = duree;
playmusic();
_currentTime = x;
this.initializeDisplayTimer(_currentTime);
x--;
_intervalEnd = DateTime.Now.Add(_intervalRemainingTime);
_timer.Start();
}
}
private void Button_Click_Start(object sender, RoutedEventArgs e)
{
if (TimeSpan.Equals(_intervalRemainingTime, TimeSpan.Zero))
{
playmusic();
this.initializeDisplayTimer(Task);
_timer.Start();
}
}
private void Button_Click_Pause(object sender, RoutedEventArgs e)
{
_isRunning = false;
_timer.Stop();
}
private void Button_Click_Reset(object sender, RoutedEventArgs e)
{
_timer.Stop();
this.initializeDisplayTimer(_currentTime);
}
}
预计进入暂停2分钟后重新进入1分钟倒计时,连续5次
你的问题实际上是一个基本的c#代码逻辑问题。它不特定于 UWP 编程。如果你把整个逻辑理解清楚了,通过c#代码逻辑就很容易搞定你的target了。
我刚刚做了一个简单的代码示例供大家参考:
<Grid>
<Grid.Resources>
<Style TargetType="TextBlock">
<Setter Property="VerticalAlignment" Value="Center"></Setter>
</Style>
</Grid.Resources>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<TextBlock Grid.Column="0" Text="Count Down Time: "></TextBlock>
<TextBlock x:Name="countDown1" Grid.Column="1"></TextBlock>
<TextBlock Grid.Row="1" Grid.Column="0" Text="Pause Remaning: "></TextBlock>
<TextBlock x:Name="pauseRemaning" Grid.Row="1" Grid.Column="1"></TextBlock>
<TextBlock Grid.Row="2" Grid.Column="0" Text="Looping Times: "></TextBlock>
<TextBlock x:Name="loop_Times" Grid.Row="2" Grid.Column="1"></TextBlock>
</Grid>
public sealed partial class MainPage : Page
{
private int countDowm = 60;
private int RemainingTime = 120;
private int loopTimes = 0;
public MainPage()
{
this.InitializeComponent();
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(1);
timer.Tick += Timer_Tick;
timer.Start();
}
private void Timer_Tick(object sender, object e)
{
if (loopTimes == 5)
{
countDowm = 60;
RemainingTime = 120;
loopTimes = 0;
}
else
{
if (countDowm <= 0)
{
if (RemainingTime <= 0)
{
loopTimes++;
countDowm = 60;
RemainingTime = 120;
}
else
{
RemainingTime--;
}
}
else
{
countDowm--;
}
}
countDown1.Text = countDowm.ToString();
pauseRemaning.Text = RemainingTime.ToString();
loop_Times.Text = loopTimes.ToString();
}
}