Reputation: 13
I am trying to code a timer in UWP. I want my timer to do a loop which counts down for example 1 minutes, then counts down a 2 minute pause, all that 5 times in a row, without having to re-press the start button. I am able to count down the 1 minutes and get into the 2 minute pause but I can't figure out how to get back into the 1 minutes count down. The loop is just stuck in the 2 minute pause.
I have tried to do a IF, ELSE. I have tried 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);
}
}
It's expected to go into the pause for 2 minutes and then get back into the 1 minute countdown, 5 times in a row
Upvotes: 0
Views: 607
Reputation: 8591
Your question actually is a basic c# code logic issue. It's not specific to UWP programming. If you understand the whole logic clearly, it's easy to get your target by c# code logic.
I just made a simple code sample for your reference:
<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();
}
}
Upvotes: 1