在Xamarin.forms中的多个页面之间导航?

时间:2017-02-20 07:41:11

标签: android navigation xamarin.forms

我有一个内容视图和左侧导航抽屉,它们是4页的公共文件,内容视图包含4个图标,可在4个不同的页面之间导航。我想在所有4页上保留我的内容视图和左侧导航抽屉。 我创建一个主详细信息页面并将主页面设置为左导航抽屉,我每次都在更改详细信息页面。 我得到了异常,因为android可以在多个页面之间导航时一次只导航页面。 以下是我的Rootpage和ContentView页面

 public class RootPage : MasterDetailPage
    {
        LeftNavigationPanel menuPage;
        public RootPage(string detailSel)
        {
            menuPage = new LeftNavigationPanel(); //This is the left navigation class. rename later.
            Master = menuPage;
            if (detailSel.Equals(""))
            {

                var detail = new NavigationPage(new Tabpage());
                Detail = detail; //homepage
                detail.Icon = "leftnav.png";
                App.navigation = detail.Navigation;
            }
            else if (detailSel.Equals("1"))
            {

                var detail = new NavigationPage(new Post());
                Detail = detail; //homepage
               detail.Icon = "leftnav.png";
               App.navigation = detail.Navigation;
             //  System.Diagnostics.Debug.WriteLine("page1: " + Navigation.NavigationStack[Navigation.NavigationStack.Count-1]);

            }
            else if (detailSel.Equals("2"))
            {
                var detail = new NavigationPage(new TrackTabPage());
                Detail = detail; //homepage
                detail.Icon = "leftnav.png";
                App.navigation = detail.Navigation;
               // System.Diagnostics.Debug.WriteLine("page1: " + Navigation.NavigationStack[Navigation.NavigationStack.Count - 1]);

            }
            }
    }

内容查看

public partial class HomeContentView : ContentView
    {
        public HomeContentView()
        {
            InitializeComponent();
        }
        private async void read_click(Object sender, EventArgs e)
        {
            if (!((Navigation.NavigationStack[Navigation.NavigationStack.Count - 1]) is Tabpage))
            {

                //await Navigation.PopAsync();
                await Navigation.PushAsync(new RootPage(""));
                // this.Navigation.PopAsync();
            }
        }
        private async void post_click(Object sender, EventArgs e)
        {
            if (!((Navigation.NavigationStack[Navigation.NavigationStack.Count - 1]) is Post))
            {
                System.Diagnostics.Debug.WriteLine("page: "+ Navigation.NavigationStack[Navigation.NavigationStack.Count - 1]);
              //  await Navigation.PopAsync();
                await Navigation.PushAsync(new RootPage("1"));


            }
        }
        private async void track_click(Object sender, EventArgs e)
        {
            if (!((Navigation.NavigationStack[Navigation.NavigationStack.Count - 1]) is TrackTabPage))
            {
                System.Diagnostics.Debug.WriteLine("page: " + Navigation.NavigationStack[Navigation.NavigationStack.Count - 1]);
              //  await Navigation.PopAsync();
                await Navigation.PushAsync(new RootPage("2"));
            }
        }
        private void play_click(Object sender, EventArgs e) { }
    }

1 个答案:

答案 0 :(得分:0)

您应该使用详情页面中的Navigation,因为您将详细信息设置为new NavigationPage()

我通常使用以下代码来INavigation在我的应用内导航:

public static INavigation GetNavigation()
{
    var mdp = Application.Current.MainPage as MasterDetailPage;
    if (mdp != null)
    {
        // Return the navigation of the detail-page
        return mdp.Detail.Navigation;
    }

    var np = Application.Current.MainPage as NavigationPage;
    if (np != null)
    {
        // Page is no MasterDetail-Page, but has a navigation
        return np.Navigation;
    }

    // No navigation found (just set the page, instead of navigation)
    return null;
}

并使用它,如下例所示:

var nav = GetNavigation();
if(nav != null)
{
    await nav.PushAsync(new Tabpage());
}
else
{
    // Just set the page, because there is no navigation
    Application.Current.MainPage = new new Tabpage();

    // Or like this
    Application.Current.MainPage = new NavigationPage(new Tabpage());
}

<强>更新

我稍微改变了我的回答(感谢评论,我只是没有意识到这一点)。请勿使用new Rootpage("")更改页面。有了这个,你可以在当前导航的基础上创建一个新的导航..这没有任何意义。

Push页面放到GetNavigation()上的当前导航栏中。

请阅读Xamarin.Forms中的documentation about the navigation pattern以获取更多信息。