【问题标题】:Xamarin Navigation IssueXamarin 导航问题
【发布时间】:2021-05-08 22:29:22
【问题描述】:

我有一个我无法理解的问题,我想从 MainPage 导航到另一个页面 (CheckPage),然后我想回到 MainPage,程序上它可以工作,但是当顶部有一个空白时我回到MainPage,看截图

主页代码

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="xamrinApp.MainPage">

<StackLayout>

    <Grid BackgroundColor="DimGray" HeightRequest="100">
        <Label Text="Xamarin App" FontSize="22" Margin="0,50,0,0" HorizontalTextAlignment="Center" TextColor="White"/>
    </Grid>

    <Label Text="Welcome to Xamarin application" FontSize="Title" Padding="30,10,30,10"/>
    <Label Text="Please select from the list:" FontSize="16" Padding="30,0,30,0"/>

    
    <Entry x:Name="enRegistrationNumber" Margin="10"></Entry>
   <Button x:Name="btnClickMe" Text="Fetch" Clicked="btnClickMe_Clicked" />

    <Label x:Name="laResult" Text="..." HorizontalTextAlignment="Center"></Label>

    <Button x:Name="btnCheckRecord" Text="Check Record" Clicked="btnCheckRecord_Clicked"/>

</StackLayout>

查看录制按钮代码

App.Current.MainPage  = new NavigationPage(new CheckRecordPage());

CheckRecord 页面代码

<ContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="xamrinApp.Views.CheckRecordPage">

<Grid BackgroundColor="LightYellow">
    <Button x:Name="btnBack" Text="Back" Clicked="btnBack_Clicked"></Button>
</Grid>

返回按钮代码

App.Current.MainPage = new NavigationPage(new MainPage());

【问题讨论】:

    标签: c# xaml xamarin xamarin.forms


    【解决方案1】:

    导航不是这样工作的。请阅读docs

    当您第一次在App.xaml.cs 中分配MainPage 时,将其包装在NavigationPage

    MainPage = new NavigationPage(new MyFirstPage());
    

    那么当你想从第一页导航到第二页时

    // in MyFirstPage
    this.Navigation.PushAsync(new MySecondPage());
    

    当您想要导航回第一页时,您的用户可以使用后退按钮,也可以通过编程方式完成此操作

    // in MySecondPage
    this.Navigation.PopAsync();
    

    【讨论】:

    • 当我尝试使用 PushAsync 时出现此错误“不支持 PushAsync”
    • 您在哪个平台上测试?这是您收到的确切错误消息吗?
    • “平台”是指 iOS/Android。而你没有回答我问你的另一个问题。
    • iOS 中出现错误,是的,这就是错误的确切消息
    • 您是否如第一个示例所示将您的第一页包装在 NavigationPage 中?
    【解决方案2】:

    如果您使用的是 AppShell,请尝试使用:

    Shell.Current.GoToAsync($"YourPageName");
    

    不要忘记在构造函数的 AppShell.xaml.cs 中为您的页面注册一个路由:

    Routing.RegisterRoute(nameof(YourPageName) , typeof(YourPageName));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-26
      • 2017-01-21
      • 2021-07-16
      • 2021-12-24
      相关资源
      最近更新 更多