Reputation: 53
I have been reading my code over and over for the past hour and cannot seem to understand why the changes I have added now don't allow my page to display (the page not displaying is NameEntryPage). The app starts on a the StartPage which has two buttons allowing the user to choose either One Player Game or Two Player Game. If the user chooses Two Player Game, then it navigates to NameEntryPage where the user can enter names for the two players. NameEntryPage has a button that when clicked passes the names entered and navigates to TwoPlayerPage. I have checked StartPage, OnePlayerPage, and TwoPlayerPage and they all still work. However, NameEntryPage does not load anything (and the button on StartPage that takes you there does not work either. To check the other pages I had to change the code in App() to begin on those pages)
App() code
public App()
{
InitializeComponent();
MainPage = new NavigationPage(new Views.NameEntryPage());
}
NameEntryPage.xaml code
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="SampleApp.Views.NameEntryPage">
<ContentPage.Content>
<StackLayout Margin="20">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
</Grid>
<Entry
x:Name="EntryNameP1"
Placeholder="Enter a name for Player 1"
Text="{Binding NameP1}"
Grid.Row="0" Grid.Column="0"/>
<Entry x:Name="EntryNameP2"
Placeholder="Enter a name for Player 2"
Text="{Binding NameP2}"
Grid.Row="1" Grid.Column="0"/>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Button Grid.Column="0"
Text="Start"/>
</Grid>
</StackLayout>
</ContentPage.Content>
code behind
namespace SampleApp.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class NameEntryPage : ContentPage
{
public NameEntryPage()
{
NavigationPage.SetHasNavigationBar(this, false);
InitializeComponent();
BindingContext = new NameEntryPageViewModel(Navigation, EntryNameP1.Text, EntryNameP2.Text);
}
}
}
VM code
public class NameEntryPageViewModel : INotifyPropertyChanged
{
public INavigation Navigation { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
{
var changed = PropertyChanged;
if (changed == null)
return;
changed.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private string nameP1;
public string NameP1
{
get
{
return NameP1;
}
set
{
if (NameP1 != value)
{
nameP1 = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("NameP1"));
}
}
}
private string nameP2;
public string NameP2
{
get
{
return NameP2;
}
set
{
if (NameP2 != value)
{
nameP2 = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("NameP1"));
}
}
}
public Command StartGameCommand { get; set; }
public NameEntryPageViewModel(INavigation navigation, string name1, string name2)
{
nameP1 = name1;
nameP2 = name2;
this.Navigation = navigation;
StartGameCommand = new Command(() => StartGame(nameP1, nameP2));
}
public NameEntryPageViewModel(INavigation navigation)
{
nameP1 = "Player 1";
nameP2 = "Player 2";
this.Navigation = navigation;
StartGameCommand = new Command(() => StartGame(nameP1, nameP2));
}
private void StartGame(string name1, string name2)
{
this.Navigation.PushAsync(new Views.TwoPlayerPage(name1, name2));
}
}
Upvotes: 0
Views: 27
Reputation: 89204
this will create an infinite loop
public string NameP1
{
get
{
return NameP1;
}
NameP1
will call the getter for NameP1
, which calls the getter for NameP1
, recursively until you crash
instead your getter should return the private nameP1
variable
public string NameP1
{
get
{
return namep1;
}
Upvotes: 1