尽管没有错误,页面根本不会显示
Posted
技术标签:
【中文标题】尽管没有错误,页面根本不会显示【英文标题】:Page will not display at all despite no errors 【发布时间】:2020-03-22 20:25:27 【问题描述】:过去一个小时我一直在反复阅读我的代码,似乎无法理解为什么我现在添加的更改不允许我的页面显示(未显示的页面是 NameEntryPage)。该应用程序从具有两个按钮的 StartPage 开始,允许用户选择 One Player Game 或 Two Player Game。如果用户选择两个玩家游戏,那么它导航到 NameEntryPage,用户可以在其中输入两个玩家的名称。 NameEntryPage 有一个按钮,单击该按钮会传递输入的名称并导航到 TwoPlayerPage。我已经检查了 StartPage、OnePlayerPage 和 TwoPlayerPage,它们仍然有效。但是,NameEntryPage 不会加载任何内容(并且 StartPage 上的带您到那里的按钮也不起作用。要检查其他页面,我必须更改 App() 中的代码才能从这些页面开始)
App() 代码
public App()
InitializeComponent();
MainPage = new NavigationPage(new Views.NameEntryPage());
NameEntryPage.xaml 代码
<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>
后面的代码
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);
虚拟机代码
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));
【问题讨论】:
【参考方案1】:这将创建一个无限循环
public string NameP1
get
return NameP1;
NameP1
会调用NameP1
的getter,它会递归调用NameP1
的getter,直到你崩溃
您的 getter 应该返回 private nameP1
变量
public string NameP1
get
return namep1;
【讨论】:
以上是关于尽管没有错误,页面根本不会显示的主要内容,如果未能解决你的问题,请参考以下文章