简体   繁体   English

Xamarin.Forms中的导航不起作用

[英]Navigation in Xamarin.Forms not working

The navigation doesn't work by setting the Mainpage as the navigation page or by setting the Mainpage to be another content page as i tried the solution Here: How to navigate one Content page to another Content page from client project (IOS/Android) in xamarin forms? 导航不起作用,方法是将Mainpage设置为导航页面,或者将Mainpage设置为另一个内容页面,因为我尝试了以下解决方案: 如何在客户端项目(IOS / Android)中将一个Content页面导航到另一个Content页面Xamarin形式?

using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace App14
{
public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
        Application.Current.MainPage = new Page1();


    }


}
}

If I run the above code i don't see any Content The app just load and terminates 如果我运行上面的代码,我看不到任何内容。该应用只会加载并终止

    using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Text;
    using System.Threading.Tasks;
   using Xamarin.Forms;

   namespace App14
   {
   public partial class MainPage : ContentPage
  {
    public MainPage()
     {
        InitializeComponent();
        App.Current.MainPage = new NavigationPage();
        Application.Current.MainPage.Navigation.PushAsync(new Page1())




    }


}
 }

and if tried making the MainPage a navigation Page and Pushasync code it won't work either 如果尝试使MainPage成为导航页和Pushasync代码,则也将无法正常工作

Your App.xaml.cs , which inherits from Application should be loading your MainPage and for navigation, it should be wrapped in a NavigationPage . Application继承的App.xaml.cs应该正在加载MainPage并且为了进行导航,应该将其包装在NavigationPage

public partial class App : Application
{
    public App()
    {
        InitializeComponent();

        var navigationPage = new NavigationPage(new MainPage());
        MainPage = navigationPage;
    }

    ...
}

Looks like you want to lauch your app having its root page to be Page1 . 看来您想取消您的应用程序,其根页面为Page1

If this is true then you are writing navigation logic at a wrong place. 如果这是真的,那么您在错误的地方编写了导航逻辑。 You should remove navigation logic from your MainPage's constructor and write your first page navigation into App.cs class as follows: 您应该从MainPage's构造函数中删除导航逻辑,并将第一页导航写入App.cs类,如下所示:

public partial class App : Application
{
    public App ()
    {
        InitializeComponent ();
        MainPage = new NavigationPage(new Page1 ());
    }
}

Otherwise , 否则

If you want to launch your app with having MainPage as your root page and then immediately want to push Page1 onto navigation stack then: 如果要以MainPage作为根页面启动应用程序,然后立即将Page1推入导航堆栈,则:

public partial class App : Application
{
    public App ()
    {
        InitializeComponent ();

        var navPage = new NavigationPage(new App14.MainPage()); 
        Application.Current.MainPage = navPage; 

        navPage.PushAsync(new Page1());
    }
}

For more detail on navigation in Xamarin.Forms click here . 有关Xamarin.Forms中导航的更多详细信息, 请单击此处

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

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