简体   繁体   English

Xamarin 导航问题

[英]Xamarin Navigation Issue

I'm have an issue that I can not understand, I want to navigate from MainPage to another page (CheckPage), Then I want to back to the MainPage, Programmatly it's work but there is a white space from the top when I back to the MainPage, See the screenshot我有一个我无法理解的问题,我想从 MainPage 导航到另一个页面(CheckPage),然后我想回到 MainPage,Programmatly 它可以工作,但是当我返回时顶部有一个空白区域主页,见截图

在此处输入图像描述

MainPage Code主页代码

<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>

Check record button code检查记录按钮代码

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

CheckRecord page code检查记录页面代码

<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>

Back button code返回按钮代码

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

That's not how navigation works.这不是导航的工作方式。 Please read the docs请阅读文档

when you first assign the MainPage in App.xaml.cs , wrap it in a NavigationPage当您第一次在App.xaml.cs中分配MainPage时,将其包装在NavigationPage

MainPage = new NavigationPage(new MyFirstPage());

then when you want to navigation from the first page to the second page那么当你想从第一页导航到第二页时

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

when you want to navigate back to the first page, your user can either use the back button, or you can do this programatically当您想要导航回第一页时,您的用户可以使用后退按钮,也可以通过编程方式执行此操作

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

If you are using the AppShell try to navigate with:如果您使用的是 AppShell,请尝试使用以下方式导航:

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

Don't forget to register a route for your page in AppShell.xaml.cs in the constructor:不要忘记在构造函数的 AppShell.xaml.cs 中为您的页面注册一个路由:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM