I'm kind of new to .net maui and c# in general and im trying to program a wordle game and a problem ive encountered is when using databinding on my main page a few problems have arisen:
The main page is the only page you can access when the program runs, like the app is literally locked on it The button i have to navigate away from the page no longer works its just there And the function I have to get a random word out of a long list from github no longer fires. Im using the community toolkit and I should have all the correct stuff for it to work properly
Any help would be greatly appreciated thanks.
<?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>
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());
}
}
}
//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; }
}
}
I found that your code in the
App.xaml.csfile is different from mine. SettingWelcomePagetoMainPagewill cause the project to only display theWelcomePagepage.Since you used the Shell architecture in the project, you can try setting
AppShelltoMainpagein App.xaml.cs file like the following code.