From 69f39fb84c2011425c203c2b5d23a260745dbe05 Mon Sep 17 00:00:00 2001 From: timunie Date: Mon, 9 Dec 2024 22:19:18 +0100 Subject: [PATCH] WIP --- .../ViewModels/SnowFlakeGameViewModel.cs | 26 +++++----- .../Views/MainWindow.axaml | 51 ++++++++++--------- .../MVVM/BasicMvvmSample/README.adoc | 4 +- 3 files changed, 43 insertions(+), 38 deletions(-) diff --git a/src/Avalonia.Samples/CustomControls/SnowFlakesControlSample/ViewModels/SnowFlakeGameViewModel.cs b/src/Avalonia.Samples/CustomControls/SnowFlakesControlSample/ViewModels/SnowFlakeGameViewModel.cs index 517d995..687073a 100644 --- a/src/Avalonia.Samples/CustomControls/SnowFlakesControlSample/ViewModels/SnowFlakeGameViewModel.cs +++ b/src/Avalonia.Samples/CustomControls/SnowFlakesControlSample/ViewModels/SnowFlakeGameViewModel.cs @@ -1,5 +1,6 @@ using System; using System.Collections.ObjectModel; +using System.Diagnostics; using Avalonia.Threading; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; @@ -11,35 +12,34 @@ public partial class SnowFlakeGameViewModel : ViewModelBase { public SnowFlakeGameViewModel() { - _gameTimer = new DispatcherTimer(TimeSpan.FromMilliseconds(50), DispatcherPriority.Background, OnGameTimerTick); + _gameTimer = new DispatcherTimer(TimeSpan.FromMilliseconds(100), DispatcherPriority.Background, OnGameTimerTick); } private void OnGameTimerTick(object? sender, EventArgs e) { - OnPropertyChanged(nameof(TimeRemaining)); - OnPropertyChanged(nameof(SecondsRemaining)); + OnPropertyChanged(nameof(MilliSecondsRemaining)); - if (TimeRemaining < TimeSpan.Zero) + if (MilliSecondsRemaining <= 0) { _gameTimer.Stop(); + _stopwatch.Stop(); IsGameRunning = false; } } private readonly DispatcherTimer _gameTimer; - private DateTime _gameStartTime = DateTime.MinValue; - + private readonly Stopwatch _stopwatch = new Stopwatch(); + public ObservableCollection SnowFlakes { get; } = new(); [ObservableProperty] private bool _isGameRunning; - [ObservableProperty] private int _Score; + [ObservableProperty] private int _score; - [ObservableProperty] [NotifyPropertyChangedFor(nameof(SecondsGameDuration))] - TimeSpan _GameDuration = TimeSpan.Zero; + [ObservableProperty] [NotifyPropertyChangedFor(nameof(MilliSecondsGameDuration))] + private TimeSpan _gameDuration = TimeSpan.Zero; - public TimeSpan TimeRemaining => _gameStartTime + GameDuration - DateTime.Now; - public double SecondsRemaining => TimeRemaining.TotalSeconds; - public double SecondsGameDuration => GameDuration.TotalSeconds; + public double MilliSecondsRemaining => (GameDuration - _stopwatch.Elapsed).TotalMilliseconds; + public double MilliSecondsGameDuration => GameDuration.TotalMilliseconds; [RelayCommand] private void StartGame() @@ -56,7 +56,7 @@ private void StartGame() Random.Shared.NextDouble() / 5 + 0.1)); } - _gameStartTime = DateTime.Now; + _stopwatch.Restart(); GameDuration = TimeSpan.FromMinutes(1); IsGameRunning = true; _gameTimer.Start(); diff --git a/src/Avalonia.Samples/CustomControls/SnowFlakesControlSample/Views/MainWindow.axaml b/src/Avalonia.Samples/CustomControls/SnowFlakesControlSample/Views/MainWindow.axaml index 0690b51..0f8ae3f 100644 --- a/src/Avalonia.Samples/CustomControls/SnowFlakesControlSample/Views/MainWindow.axaml +++ b/src/Avalonia.Samples/CustomControls/SnowFlakesControlSample/Views/MainWindow.axaml @@ -10,15 +10,6 @@ Icon="/Assets/avalonia-logo.ico" Title="SnowFlakesControl"> - - - - - - - - - @@ -26,27 +17,39 @@ + + + + + + + + + + + - - + + + + Value="{Binding MilliSecondsRemaining}" /> + + - -