.NET MAUI绑定MainPage后页面锁定、导航失效等问题求助
.NET MAUI Wordle游戏数据绑定后出现问题求助
我是.NET MAUI和C#新手,正在开发一款Wordle游戏。为主页面(MainPage)添加数据绑定后遇到了以下问题:
- 程序运行后始终停留在主页面,无法跳转到其他页面
- 用于跳转的按钮完全失效
- 从GitHub单词列表获取随机单词的函数无法执行
已使用Community Toolkit,相关配置应该无误,恳请各位帮忙排查问题。
MainPage.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" x:Class="Wordle.MainPage" xmlns:viewmodel="clr-namespace:Wordle.ViewModel" xmlns:models="clr-namespace:Wordle.Models" x:DataType="viewmodel:WordleViewModel" Title="Welcome to Wordle"> <ScrollView> <VerticalStackLayout Spacing="30" Padding="20" VerticalOptions="Center"> <!--Content of our main page--> <Image Source="gametitle.png" HeightRequest="150"/> <Grid RowDefinitions="*, Auto"> <VerticalStackLayout BindableLayout.ItemsSource="{Binding CurrentRow}"> <BindableLayout.ItemTemplate> <DataTemplate x:DataType="models:GameRows"> <HorizontalStackLayout BindableLayout.ItemsSource="{Binding CorrectLetters}" HorizontalOptions="Center"> <BindableLayout.ItemTemplate> <DataTemplate x:DataType="models:Letters"> <Button Style="{DynamicResource LettersColor}" Text="Test" BackgroundColor="{Binding bgCol}"/> </DataTemplate> </BindableLayout.ItemTemplate> </HorizontalStackLayout> </DataTemplate> </BindableLayout.ItemTemplate> </VerticalStackLayout> </Grid> <Button x:Name="signInbtn" Text="?" Clicked="signIn" WidthRequest="100" BackgroundColor="Gray" TextColor="White"/> </VerticalStackLayout> </ScrollView> </ContentPage>
MainPage.xaml.cs
using System; using System.Collections.ObjectModel; using System.Net.Http; using System.ComponentModel; using System.Runtime.CompilerServices; using Microsoft.Maui.Storage; using Wordle.ViewModel; namespace Wordle { public partial class MainPage : ContentPage { //Declaring Variables private Random rand; private List<string> gameWords; private HttpClient http; private string userGuess; private string correctWord; private int gamesPlayed; private int numWords; public string userName; private string password; public string targetFile; public bool isBusy; public MainPage(WordleViewModel newView) { InitializeComponent(); BindingContext = newView; gameWords = new List<string>(); rand = new Random(); numWords = gameWords.Count; Task task = getGameWords(); DisplayAlert("Test", correctWord, "Okay"); } public MainPage() { InitializeComponent(); BindingContext = new WordleViewModel(); gameWords = new List<string>(); rand = new Random(); numWords = gameWords.Count; Task task = getGameWords(); DisplayAlert("Test", correctWord, "Okay"); } public async Task getGameWords() { targetFile = System.IO.Path.Combine(FileSystem.Current.AppDataDirectory, "words.txt"); if (!File.Exists(targetFile)) { http = new HttpClient(); var response = await http.GetAsync("https://raw.githubusercontent.com/DonH-ITS/jsonfiles/main/words.txt"); if (response.IsSuccessStatusCode) { string contents = await response.Content.ReadAsStringAsync(); using (StreamWriter writer = new StreamWriter(targetFile)) { writer.Write(contents); } using (StreamReader s = new StreamReader(targetFile)) { string line = ""; while ((line = s.ReadLine()) != null) { gameWords.Add(line); Console.WriteLine(line); numWords++; } } getRandWord(); } } else { using (StreamReader s = new StreamReader(targetFile)) { string line = ""; while ((line = s.ReadLine()) != null) { gameWords.Add(line); Console.WriteLine(line); numWords++; } } getRandWord(); } } public void getRandWord() { rand = new Random(); correctWord = gameWords[rand.Next(gameWords.Count)]; } private void signIn(object sender, EventArgs e) { Navigation.PushAsync(new SignIn()); } } }
WordleViewModel.cs
//This is the viewModel im using using CommunityToolkit.Mvvm.ComponentModel; using Wordle.Models; using CommunityToolkit.Mvvm.Input; namespace Wordle.ViewModel { public partial class WordleViewModel: ObservableObject { //Declaring Variables [ObservableProperty] public GameRows[] currentRow; private int currentRowIndex; private int currentColumnIndex; char[] answer; char[] userGuess; //Constructor to initialize some of the aspects of the ViewModel public WordleViewModel() { currentRow = new GameRows[6] { new GameRows(), new GameRows(), new GameRows(), new GameRows(), new GameRows(), new GameRows() }; //Testing storing the correct answer answer = "tests".ToCharArray(); } //Getting the user to the next time every time they hit enter //[ICommand] public void nextLine() { //Controlling when to move the user to the next row var isValid = true; if(isValid) { if(currentColumnIndex != 5) { return; } if(currentRowIndex == 5) { } else { currentRowIndex++; currentColumnIndex = 0; } } } //[ICommand] public void userEntry() { if(currentColumnIndex == 5) { return; } } } }
模型代码
//This is the model im using using CommunityToolkit.Mvvm.ComponentModel; namespace Wordle.Models { public class GameRows { //Declaring Variables int currentRow; int currentCol; char[] answer; char[] userGuess; //Constructor to initialize some of the variables of the Model class public GameRows() { CorrectLetters = new Letters[5] { new Letters(), new Letters(), new Letters(), new Letters(), new Letters(), }; } //Correct Letters public Letters[] CorrectLetters { get; set; } public void ValidateAnswer(char[] correctWord) { } } public partial class Letters : ObservableObject { [ObservableProperty] private char userInput; [ObservableProperty] private Color colorChange; public char CorrectLetter { get; set; } public Color bgCol { get; set; } } }
内容的提问来源于stack exchange,提问作者Eoin
相关产品推荐
相关产品推荐

