我正在使用 .NET MAUI MVVM 开发一个简单的数学练习游戏。我希望在每次玩游戏时关闭应用程序并重新输入后更新游戏主页上的高分值。什么页面以及如何执行此操作?
PlayerModal.cs
using CommunityToolkit.Mvvm.ComponentModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MauiApp2.Models
{
public partial class PlayerModal : ObservableObject
{
[ObservableProperty]
int score;
[ObservableProperty]
int highScore;
[ObservableProperty]
int timeReaming;
[ObservableProperty]
int correctAnswer;
[ObservableProperty]
string answer;
[ObservableProperty]
string question;
}
}
乘法PageViewModel.cs
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using MauiApp2.Models;
using MauiApp2.Services;
using MauiApp2.Views;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
namespace MauiApp2.ViewModels
{
public partial class MultiplicationPageViewModel : BaseViewModel
{
public ObservableCollection<PlayerModal> PlayerDetails { get; set; } = new ObservableCollection<PlayerModal>();
public PlayerModal playerModal { get; set; } = BaseViewModel.PlayerModal;
public ICommand SubmitAnswerCommand { get; }
public MultiplicationPageViewModel(INavigationService navigationService) : base(navigationService)
{
playerModal.Score = 0;
playerModal.TimeReaming= 60;
SubmitAnswerCommand = new Command(SubmitAnswer);
// Create a question as an example
GenerateQuestion();
// Start the timer
StartTimer();
}
[RelayCommand]
public void GenerateQuestion()
{
Random random = new Random();
int number1 = random.Next(1, 11); // a random number between 1 and 10
int number2 = random.Next(1, 11); // a random number between 1 and 10
// playerModal.Answer = (number1 * number2).ToString(); Automatic Answer
playerModal.Question = $"{number1} x {number2}";
playerModal.CorrectAnswer = number1 * number2;
}
public async void StartTimer()
{
while (true)
{
await Task.Delay(1000); // wait 1 sec
playerModal.TimeReaming--;
if (playerModal.TimeReaming == 0)
{
GameOver(); // Finish the game when time is up
playerModal.TimeReaming = 60;
}
}
}
public void SubmitAnswer()
{
if (int.TryParse(playerModal.Answer, out int userAnswer))
{
int correctAnswer = playerModal.CorrectAnswer;
if (userAnswer == correctAnswer)
{
playerModal.Score += 10;
playerModal.TimeReaming += 5;
}
else
{
playerModal.Score -= 10;
playerModal.TimeReaming -= 55;
// Negative score and time control
if (playerModal.Score < 0)
{
playerModal.Score = 0;
}
if (playerModal.TimeReaming <= 0)
{
GameOver();
playerModal.TimeReaming = 60;
}
}
GenerateQuestion();
}
playerModal.Answer = "";
}
public void GameOver()
{
// Check earned score and update new high score
if (playerModal.Score > playerModal.HighScore)
{
playerModal.HighScore = playerModal.Score;
// Save the high score to preferences
Preferences.Default.Set("HighScore", playerModal.HighScore);
}
NavigationService.NavigateToAsync(nameof(GameOverPage));
}
}
}
HomePageViewModel.cs
using CommunityToolkit.Mvvm.Input;
using MauiApp2.Models;
using MauiApp2.Services;
using MauiApp2.Views;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MauiApp2.ViewModels
{
public partial class HomePageViewModel : BaseViewModel
{
public ObservableCollection<PlayerModal> PlayerDetails { get; set; } = new ObservableCollection<PlayerModal>();
public PlayerModal playerModal { get; set; } = BaseViewModel.PlayerModal;
public HomePageViewModel(INavigationService navigationService) : base(navigationService)
{
// Load the high score from preferences
int highScore = Preferences.Default.Get("HighScore", 0);
// Set the high score on the view model
playerModal.HighScore = highScore;
}
[RelayCommand]
public void Play()
{
NavigationService.NavigateToAsync(nameof(SelectionPage));
}
}
}
主页.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:viewModels="clr-namespace:MauiApp2.ViewModels"
xmlns:models="clr-namespace:MauiApp2.Models"
x:Class="MauiApp2.Views.HomePage"
x:DataType="viewModels:HomePageViewModel"
Title="Home">
<VerticalStackLayout VerticalOptions="Center">
<Label
Text="MATH GAME"
VerticalOptions="Center"
HorizontalOptions="Center"
FontAttributes="Bold"
FontSize="50"/>
<Label
Text="High Score"
Margin="0,15,0,0"
VerticalOptions="Center"
HorizontalOptions="Center"
FontAttributes="None"
FontSize="40"/>
<Label
Text="{Binding playerModal.HighScore}"
VerticalOptions="Center"
HorizontalOptions="Center"
FontAttributes="Bold"
FontSize="30"/>
<Button Text="Play"
FontAttributes="Bold"
FontSize="20"
Margin="90,30,90,0"
Command="{Binding PlayCommand}"/>
</VerticalStackLayout>
</ContentPage>
使用首选项
和