【问题标题】:MainPage not added to the Navigation StackMainPage 未添加到导航堆栈
【发布时间】:2017-05-13 14:03:17
【问题描述】:

我只需调用以下命令即可启动我的 Xamarin.Forms android 项目:

    MainPage = new NavigationPage(new HomePage());

在 App.xaml.cs 中

https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/navigation/hierarchical/中所述

这会导致 HomePage ContentPage 实例被推送到 导航堆栈,它成为活动页面和根页面 应用程序。

但是当我检查堆栈时,它是空的:

这里发生了什么?为什么页面不在堆栈中?

【问题讨论】:

  • 您看到显示的页面了吗?
  • @AlessandroCaliaro 是的,我愿意,一切似乎都很好,我只是好奇为什么不是他们在文档中指定的方式
  • 当你看“计数”的时候?紧随 MainPage = new NavigationPage(new HomePage()); ???
  • 我测试了一些放置在代码中、另一个模态页面以及MainPage的其他方法中的某个地方。到处都是一样的结果

标签: c# android navigation xamarin.forms


【解决方案1】:

我刚刚创建了一个新的 XF 项目。

我已经把主页xml改成了这个

<?xml version="1.0" encoding="utf-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:Test" x:Class="Test.TestPage">
    <StackLayout>
        <Button Text="How many?" Clicked="Handle_Clicked"/>
        <Label Text="Welcome to Xamarin Forms!" VerticalOptions="Center" HorizontalOptions="Center" />
    </StackLayout>
</ContentPage>

并将其添加到 xml.cs

void Handle_Clicked(object sender, System.EventArgs e)
{
    //throw new NotImplementedException();
    Application.Current.MainPage.DisplayAlert("Attention", Navigation.NavigationStack.Count.ToString(), "Ok");
}

我看到 NavigationStack 的计数是 1...

更新

还有一个 CarouselPage,我有同样的结果

<?xml version="1.0" encoding="UTF-8"?>
<CarouselPage xmlns="http://xamarin.com/schemas/2014/forms" 
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
    x:Class="Test.TestPage">
    <ContentPage>
        <StackLayout>
            <Button Text = "how many?" Clicked="Handle_Clicked"/>
            <Label Text="Red" />
            <BoxView Color="Red" VerticalOptions="FillAndExpand" />
        </StackLayout>
    </ContentPage>
    <ContentPage>
        <StackLayout>
            <Label Text="Green" />
            <BoxView Color="Green" VerticalOptions="FillAndExpand" />
        </StackLayout>
    </ContentPage>
    <ContentPage>
        <StackLayout>
            <Label Text="Blue" />
            <BoxView Color="Blue" VerticalOptions="FillAndExpand" />
        </StackLayout>
    </ContentPage>
</CarouselPage>

【讨论】:

  • 嗨亚历山德罗。我有@Robert Golusiński 的同样问题。 Navigation.NavigationStack.Count 始终为 0。它可能是什么?
  • 很难说……你能在github上创建一个repo让我看看吗?
  • 感谢您的回答。我检查了错误的堆栈。一切都在模态中。很抱歉打扰您。
猜你喜欢
  • 2020-05-07
  • 2023-04-03
  • 1970-01-01
  • 1970-01-01
  • 2022-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-08
相关资源
最近更新 更多