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



我有一个内容视图,左导航抽屉是4页上的共享,内容视图包含4个图标,可在4个不同的页面之间导航。我想保留我的内容视图,并在所有4页上左导航抽屉。我创建一个主详细信息页面,并将主页设置为左导航抽屉,每次都会更改详细信息页面。我得到了例外,因为Android可以一次在多个页面之间导航时一次仅导航页面。以下是我的Rootpage和ContentView Page

 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]);
            }
            }
    }

contentview

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) { }
    }

您应该使用细节页面中的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());
}

update

我更改了答案(感谢您的评论,我只是没有意识到这一点)。请勿使用new Rootpage("")更改页面。因此,您可以在当前导航的顶部创建一个新的导航。

只需Push您的页面到您通过GetNavigation()获得的当前导航。

请阅读有关Xamarin.forms中导航模式的文档以获取更多信息。

相关内容

  • 没有找到相关文章

最新更新