微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Xamarin 导航问题

如何解决Xamarin 导航问题

我有一个我无法理解的问题,我想从 MainPage 导航到另一个页面 (CheckPage),然后我想回到 MainPage,以编程方式它可以工作,但是当我回到主页面,看截图

enter image description here

主页代码

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

    
    <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());

检查记录页面代码

<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());

解决方法

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

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

MainPage = new NavigationPage(new MyFirstPage());

然后当你想从第一页导航到第二页

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

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

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

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

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

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。