即使没有错误页面也不会显示

时间:2020-03-22 20:25:27

标签: c# xamarin

过去一个小时来,我一直在阅读代码,似乎无法理解为什么我现在添加的更改不允许显示我的页面(不显示的页面是NameEntryPage)。该应用程序从一个包含两个按钮的StartPage上启动,允许用户选择“一个玩家游戏”或“两个玩家游戏”。如果用户选择“两个玩家游戏”,则它将导航到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);
        }
    }
}

VM代码

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 个答案:

答案 0 :(得分:1)

这将创建一个无限循环

public string NameP1
{
    get
    {
        return NameP1;
    }

NameP1将递归调用NameP1的getter,而NameP1则调用该getter,直到崩溃为止

相反,您的获取者应该返回private nameP1变量

public string NameP1
{
    get
    {
        return namep1;
    }