我想从一个页面导航到另一个页面

时间:2021-02-19 14:17:44

标签: xamarin xamarin.forms xamarin.android xamarin.ios

我想在单击按钮时从选项卡式页面中的页面(不是主页)导航到页面。我试过这个:

        MainPage = new NavigationPage(new listofinterest());
        MainPage = new  TabbedPageMain()
        {
            BarBackgroundColor = Color.FromHex("#F8F8F8"),

        };

////////

public partial class listofinterest : ContentPage
{
    private async void Add(object sender, EventArgs e)
    {
        await Navigation.PushAsync(new NavigationPage(new MainPage()));
    }
}

它不起作用我收到此错误 “Android 不全面支持 PushAsync,请使用 NavigationPage。” 我需要帮助。

2 个答案:

答案 0 :(得分:0)

问题是您用 MainPage 覆盖了 TabbedPageMain。只设置一次。

首先在App.xaml.cs中设置MainPage:

MainPage = new NavigationPage(new MyPage());

然后你可以像这样从 MyPage 推送其他页面:

await Navigation.PushAsync(new MyOtherPage());

答案 1 :(得分:0)

<块引用>

它不起作用,我收到此错误“Android 不全局支持 PushAsync,请使用 NavigationPage。””我需要帮助。

根据您的代码和描述,您希望从选项卡页面之一导航到项目中的其他页面。如果是,请添加到导航堆栈中,称为 App.xaml.cs 中应用程序的根页面。

 public App()
    {
        InitializeComponent();

        MainPage =new NavigationPage( new tabbedpage.TabbedPage1());
        
    }

然后是 Tabbedpage.xaml:

<TabbedPage
x:Class="FormsSample.tabbedpage.TabbedPage1"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:FormsSample.simplecontrol">
<!--  Pages can be added as references or inline  -->
<TabbedPage.Children>
    <local:Page1 />
    <local:Page3 />
    <local:Page4 />
</TabbedPage.Children>

想要将 Page1 从 Tabbedpage 导航到其他页面。

<ContentPage
x:Class="FormsSample.simplecontrol.Page1"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
Title="page 1">
<ContentPage.Content>
    <ScrollView>
        <StackLayout x:Name="Contenttest">
            <Button
                x:Name="btn1"
                Clicked="btn1_Clicked"
                Text="navigate to another page" />
        </StackLayout>

    </ScrollView>

</ContentPage.Content>
 private void btn1_Clicked(object sender, EventArgs e)
    {
        Navigation.PushAsync(new simplecontrol.Page5());
    }

关于导航的详细信息,请参考

https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/navigation/hierarchical