Skip to content

Instantly share code, notes, and snippets.

@MartinZikmund
Created December 18, 2023 10:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MartinZikmund/4d7d7d838581b1c4e328c43f8dae5faf to your computer and use it in GitHub Desktop.
Save MartinZikmund/4d7d7d838581b1c4e328c43f8dae5faf to your computer and use it in GitHub Desktop.
private DispatcherQueueTimer _timer;
private TextBlock _countdown;
public MainPage()
{
this
.Background(new ImageBrush().ImageSource("ms-appx:///ChristmasC/Assets/Background.jpg")
.Stretch(Stretch.UniformToFill))
.Content(_countdown = new TextBlock()
.FontSize(48)
.FontWeight(FontWeights.Bold)
.TextAlignment(Microsoft.UI.Xaml.TextAlignment.Center)
.VerticalAlignment(VerticalAlignment.Center)
.HorizontalAlignment(HorizontalAlignment.Center)
.Foreground(Colors.White));
_timer = DispatcherQueue.CreateTimer();
_timer.Tick += OnTick;
_timer.Interval = TimeSpan.FromSeconds(0.5);
_timer.Start();
}
private void OnTick(object? sender, object e)
{
var christmasTime = new DateTime(DateTimeOffset.UtcNow.Year, 12, 25);
var timeUntilChristmas = christmasTime - DateTime.UtcNow;
if (timeUntilChristmas.TotalSeconds < 0)
{
_timer.Stop();
_countdown.Text("Merry Christmas!");
return;
}
else
{
_countdown.Text(
$"{timeUntilChristmas.Days} days" + Environment.NewLine +
$"{timeUntilChristmas.Hours} hours" + Environment.NewLine +
$"{timeUntilChristmas.Minutes} minutes" + Environment.NewLine +
$"{timeUntilChristmas.Seconds} seconds");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment