/// <summary> /// Displays the confirm modal and returns a <see cref="Task"/> whose result represents /// the closing of the modal. /// </summary> /// <param name="viewModel">The confirm modal view model.</param> /// <returns></returns> public Task ShowConfirmModal(ConfirmModalViewModel viewModel) { // The task that will be used to await the closing of the modal var modalClosedTaskCompletionSource = new TaskCompletionSource <bool>(); Task.Run(() => { // Run on the UI thread Application.Current.Dispatcher.Invoke(async() => { try { // Create the confirm modal var confirmModal = new ConfirmModal(); // Show it and wait for it to be dismissed await confirmModal.Show(viewModel); } finally { // Let the caller know that the modal has been closed modalClosedTaskCompletionSource.TrySetResult(true); } }); }); return(modalClosedTaskCompletionSource.Task); }
/// <summary> /// Initializes a new instance of the <see cref="GamePageViewModel"/> class with /// the specified game settings. /// </summary> /// <param name="parameter">The game settings.</param> public GamePageViewModel(GameSettings gameSettings) { // Initialize the stopwatch and run the timer that will update the time every second mStopWatch = new Stopwatch(); mTimer = new Timer(1000); mTimer.Elapsed += (o, e) => ElapsedTime = mStopWatch.Elapsed; // Create the game field and listen for when the game is started and is over GameField = new GameFieldViewModel(gameSettings); GameField.GameStarted += (o, e) => { // Start measuring the time when the game starts mStopWatch.Start(); mTimer.Start(); }; GameField.GameOver += async(o, e) => { // Stop measuring the time when the game is over mStopWatch.Stop(); mTimer.Stop(); // Show a modal with content based on the game result ConfirmModalViewModel modalViewModel; if (e.PlayerWon) { modalViewModel = new ConfirmModalViewModel("Congratulations", "You successfully finished the game", "OK"); } else { modalViewModel = new ConfirmModalViewModel("Game Over", "You clicked on a bomb", "OK"); } // And wait until the modal is exited var modalService = IoC.Get <IModalService>(); await modalService.ShowConfirmModal(modalViewModel); // Handle game over OnGameOver(); }; ExitCommand = new RelayCommand(p => OnGameOver()); }