Created
December 18, 2023 10:01
-
-
Save MartinZikmund/4d7d7d838581b1c4e328c43f8dae5faf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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