You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MAUI数学应用点击Play按钮无法跳转至GameSelectionPage求助

问题:点击Play按钮页面无法跳转

我采用MVVM设计模式开发一款基础运算数学应用,应用首页显示历史最高分信息及Play按钮。点击Play按钮后,用户应能选择想要练习的基础数学运算(如加减乘除),但目前点击该按钮后页面并未跳转。相关代码如下:

MauiProgram.cs

using MauiApp1.Sevices;
using MauiApp1.ViewModels;
using MauiApp1.Views;
using Microsoft.Extensions.Logging;

namespace MauiApp1;

public static class MauiProgram
{
    public static MauiApp CreateMauiApp()
    {
        var builder = MauiApp.CreateBuilder();
        builder
            .UseMauiApp<App>()
            .ConfigureFonts(fonts =>
            {
                fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
                fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
            });

#if DEBUG
        builder.Logging.AddDebug();

#endif
        builder.Services.AddSingleton<INavigationService, NavigationService>();

        // UI
        builder.Services.AddSingleton<HomePage>();
        builder.Services.AddTransient<GameSelectionPage>();

        // ViewModel
        builder.Services.AddSingleton<HomePageViewModel>();
        builder.Services.AddTransient<GameSelectionPageViewModel>();

        return builder.Build();
    }
}

AppShell.xaml.cs

using MauiApp1.Views;

namespace MauiApp1;

public partial class AppShell : Shell
{
    public AppShell()
    {
        InitializeComponent();
        Routing.RegisterRoute(nameof(GameSelectionPage), typeof(GameSelectionPage));
    }
}

HomePage.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:MauiApp1.ViewModels"
             x:DataType="viewModels:HomePageViewModel"
             x:Class="MauiApp1.Views.HomePage">
    <StackLayout HorizontalOptions="Center" Padding="0,250">
        <Label Text="Math Game"
               FontSize="30"
               FontAttributes="Bold"/>
        <Label Text="{Binding HighScore}" />
        <Button Text="Play" FontSize="20" FontAttributes="Bold" Command="{Binding PlayCommand}" />
    </StackLayout>
</ContentPage>

HomePage.xaml.cs

using MauiApp1.Sevices;
using MauiApp1.ViewModels;

namespace MauiApp1.Views;

public partial class HomePage : ContentPage
{
    public HomePage()
    {
        InitializeComponent();
        BindingContext = new HomePageViewModel(new NavigationService());
    }
}

HomePageViewModel.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using MauiApp1.Sevices;
using MauiApp1.Views;

namespace MauiApp1.ViewModels
{
    public class HomePageViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private int _highScore;
        public int HighScore
        {
            get => _highScore;
            set
            {
                _highScore = value;
                OnPropertyChanged(nameof(HighScore));
            }
        }

        public ICommand PlayCommand { get; }

        private INavigationService _navigationService;

        public HomePageViewModel(INavigationService navigationService)
        {
            _navigationService = navigationService;

            PlayCommand = new Command(Play);
        }

        private void Play()
        {
            // 点击Play按钮后执行的操作
            _navigationService.NavigateToAsync("GameSelectionPage");
        }

        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MauiApp1.Views;
using Microsoft.Maui.Controls;

namespace MauiApp1.Sevices
{
    public interface INavigationService
    {
        Task NavigateToAsync(string pageName);
    }
    public class NavigationService : INavigationService
    {
        private readonly Dictionary<string, Type> _pageMap = new Dictionary<string, Type>
        {
            { "HomePage", typeof(HomePage) },
            { "GameSelectionPage", typeof(GameSelectionPage) },
            { "GamePage", typeof(GamePage) },
            { "GameOverPage", typeof(GameOverPage) }
            // 添加其他页面
        };

        public async Task NavigateToAsync(string pageName)
        {
            if (_pageMap.TryGetValue(pageName, out Type pageType))
            {
                Page page = (Page)Activator.CreateInstance(pageType);
                await Application.Current.MainPage.Navigation.PushAsync(page);
            }
            else
            {
                throw new ArgumentException($"未找到名为 {pageName} 的页面", nameof(pageName));
            }
        }
    }
}

内容的提问来源于stack exchange,提问作者M3rtixius

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 15:53:17