我需要在 xamarin.Forms.app 上从一个页面导航到另一个页面

I need to navigate from one page to an another page on xamarin.Forms.app

因为我是Xamarin的新手,所以我真的不知道如何在页面之间切换。所以我参考了互联网并找到了一些解决方案。但是有一些 errors.Can 任何人帮助解决这个问题?这是我在 app.cs file.I 中的代码,对我不确定的代码添加注释。当我单击 btn1 时,触发了一个名为“iOS 全局不支持 pushAsync”的错误。

using System;
using Xamarin.Forms;

namespace mine
{
    public class App : Application
    {
    public App ()
        {

            // The root page of your application


            Button btn1 = new Button();
            Button btn2 = new Button();
            Button btn3 = new Button();
            btn1.Text= "Farmer";
            btn2.Text= "Advisor";
            btn3.Text= "Supplier";

            MainPage = new ContentPage {
                Content = new StackLayout {
                    VerticalOptions = LayoutOptions.Center,
                    Children = {
                        new Label {
                            XAlign = TextAlignment.Center,
                            Text = "Welcome to Rice Growing Advisor!"
                                //App.Navigation.PushAsync(new MyCustomContentPage());
                                //App.Navigation.PushAsync (new MainPage ());
                        }, btn1,btn2,btn3
                    }
                }

            

            };
            //var np = new NavigationPage(new MainPage());
            //MainPage=np;
            //MainPage = new NavigationPage (MainPage);
            //var rootPage = new NavigationPage(MainPage);



            btn1.Clicked += async (sender, e) => {
                //MainPage= new NavigationPage(new MainPage());
                //MainPage = np;

                await MainPage.Navigation.PushAsync(new NavigationPage());
                MainPage.Navigation.RemovePage(MainPage);
                
                


            };

        }

        protected override void OnStart ()
        {
            // Handle when your app starts
        }

        protected override void OnSleep ()
        {
            // Handle when your app sleeps
        }

        protected override void OnResume ()
        {
            // Handle when your app resumes
        }
    }
}

Navigation 是一个 属性 页。你必须调用类似

的东西
await MainPage.Navigation.PushAsync(new ExamplePage(1));

导航是 属性 VisualElement。 Button.Clicked 的所有代码都在 App class(它是 Application 的子类)的构造函数中,它没有 "Navigation" 属性。 在您的情况下,您可以使用 MainPage.Navigation(因为 MainPage 是一个页面,而页面是一个 VisualElement)。但还有一个问题:您的 MainPage 不是 NavigationPage,因此您无法执行 PushAsync(..)。要解决此问题,您应该像这样将 ContentPage 包装到导航页面中:

    var page = new ContentPage {
        Content = new StackLayout {
            VerticalOptions = LayoutOptions.Center,
            Children = {
                new Label {
                    XAlign = TextAlignment.Center,
                    Text = "Welcome to Rice Growing Advisor!"
                        //App.Navigation.PushAsync(new MyCustomContentPage());
                        //App.Navigation.PushAsync (new MainPage ());
                }, btn1,btn2,btn3
            }
        }
    };

    MainPage = new NavigationPage(page);

顺便说一句,this arcticle 将帮助您了解正在发生的事情。

编辑:

using System;   
using Xamarin.Forms;

namespace test 
{ 
    public class App : Application 
    { 
        public App () 
        {
            // The root page of your application

            // set up properties via object initializer for clarity
            Button btn1 = new Button {Text = "Farmer"};
            Button btn2 = new Button {Text = "Advisor"};
            Button btn3 = new Button {Text = "Supplier"};

            var page = new ContentPage {
                Content = new StackLayout {
                    VerticalOptions = LayoutOptions.Center,
                    Children = {
                        new Label {
                            // XAlign is obsolete, use HorizontalTextAlignment instead
                            HorizontalTextAlignment = TextAlignment.Center,
                            Text = "Welcome to Rice Growing Advisor!"
                        }, btn1,btn2,btn3
                    }
                }
            };

            // wrap our page into NavigationPage
            // if we dont do that, we'll get "PushAsync is not supported globally on iOS" exception
            MainPage = new NavigationPage(page);

            btn1.Clicked += async (sender, e) => {
                var secondPage = new ContentPage
                {
                    Content = new Label
                    {
                        Text = "Click \"BACK\" to navigate backward", 
                        HorizontalTextAlignment = TextAlignment.Center,
                        VerticalTextAlignment = TextAlignment.Center
                    }
                };
                secondPage.ToolbarItems.Add(new ToolbarItem("BACK", null, async () => { await secondPage.Navigation.PopAsync();}));

                await MainPage.Navigation.PushAsync(secondPage);
            };
        }

        protected override void OnStart ()
        {
            // Handle when your app starts
        }

        protected override void OnSleep ()
        {
            // Handle when your app sleeps
        }

        protected override void OnResume ()
        {
            // Handle when your app resumes
        }
    }
}

我希望这能正常工作。