I'm creating a countdown to a specific date. Days left, Hours left and minutes left have to be shown in a textblock.
Made a flat design in XAML, know how to calculate the timespan. Now I'm in need of a live updater while the app is running.
My Xaml
<Grid x:Name="myLayoutGrid"
Background="CadetBlue" Margin="0,-26.667,0,-0.333"
>
<TextBlock x:Name="countDays"
HorizontalAlignment="Left"
Margin="45,150,0,0"
TextWrapping="Wrap"
Text="Dagen"
VerticalAlignment="Top"
FontFamily="Tahoma"
FontSize="34" Loaded="countDays_Loaded"/>
<TextBlock x:Name="countHours"
HorizontalAlignment="Left"
Margin="45,200,0,0"
TextWrapping="Wrap"
Text="Uur"
VerticalAlignment="Top"
FontFamily="Tahoma"
FontSize="30" Loaded="countHours_Loaded"/>
<TextBlock x:Name="countMinutes"
HorizontalAlignment="Left"
Margin="45,250,0,0"
TextWrapping="Wrap"
Text="Minuten"
VerticalAlignment="Top"
FontFamily="Tahoma"
FontSize="26" Loaded="countMinutes_Loaded"/>
My code;
private void countDays_Loaded(object sender, RoutedEventArgs e)
{
DateTime end = DateTime.Parse("01/01/2016 15:00");
DateTime start = DateTime.Now;
TimeSpan ts = end - start;
countDays.Text = string.Format("{0} Dagen", ts.Days);
}
private void countHours_Loaded(object sender, RoutedEventArgs e)
{
DateTime end = DateTime.Parse("01/01/2016 15:00");
DateTime start = DateTime.Now;
TimeSpan ts = end - start;
countHours.Text = string.Format("{0} Uur", ts.Hours);
}
private void countMinutes_Loaded(object sender, RoutedEventArgs e)
{
DateTime end = DateTime.Parse("01/01/2016 15:00");
DateTime start = DateTime.Now;
TimeSpan ts = end - start;
countMinutes.Text = string.Format("{0} Minuten", ts.Minutes);
}
After i understand the code and why i should use that code, i'd like to clean up my code (put the timer in a class). After that and i studied the HUB app, i will use it in binding element.
Any help would be great.
You'll need some sort of timer to periodically refresh the remaining time. Try the follwoing approach:
public partial class MainWindow : Window
{
private readonly DateTime _endDate;
private readonly DispatcherTimer _timer;
public MainWindow()
{
InitializeComponent();
_endDate = new DateTime(2016, 1, 1, 15, 0, 0);
_timer = new DispatcherTimer();
_timer.Tick += CountDown;
_timer.Interval = TimeSpan.FromMinutes(1);
_timer.Start();
}
private void CountDown(object sender, EventArgs e)
{
var remainingTime = _endDate.Subtract(DateTime.Now);
countDays.Text = string.Format("{0} Dagen", remainingTime.Days);
countHours.Text = string.Format("{0} Uur", remainingTime.Hours);
countMinutes.Text = string.Format("{0} Minuten", remainingTime.Minutes);
}
}
To make the code compile remove the Loaded="countX_Loaded"
eventhandlers from your textblocks in your XAML.
<Window x:Class="Stackoverflow28009341.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid x:Name="myLayoutGrid" Background="CadetBlue" Margin="0,-26.667,0,-0.333">
<TextBlock x:Name="countDays"
HorizontalAlignment="Left"
Margin="45,150,0,0"
TextWrapping="Wrap"
Text="Dagen"
VerticalAlignment="Top"
FontFamily="Tahoma"
FontSize="34"/>
<TextBlock x:Name="countHours"
HorizontalAlignment="Left"
Margin="45,200,0,0"
TextWrapping="Wrap"
Text="Uur"
VerticalAlignment="Top"
FontFamily="Tahoma"
FontSize="30"/>
<TextBlock x:Name="countMinutes"
HorizontalAlignment="Left"
Margin="45,250,0,0"
TextWrapping="Wrap"
Text="Minuten"
VerticalAlignment="Top"
FontFamily="Tahoma"
FontSize="26"/>
</Grid>
</Window>