Xamarin.Forms - 主/详细页面和导航历史记录问题 [英] Xamarin.Forms - Master/detail page and navigation history issue

查看:20
本文介绍了Xamarin.Forms - 主/详细页面和导航历史记录问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,它使用 masterdetail 页面在所有页面中显示菜单.导航在我的应用程序中以两种方式发生.一种来自菜单,第二种来自仪表板.所以如果我导航到另一个页面,然后按返回"按钮,它会关闭应用程序.它不记得导航历史.主详情页面如下:

I have an app which uses masterdetail page to show menu in all page. The navigation is happened in two way in my app. one from the menu and second way from Dashboard. so if i navigate to another page, and then press "BACK" button, it closes the application. It does not remember the navigation history. The master detail page is as below:

 public class RootPage : MasterDetailPage
    {
        public RootPage ()
        {
            var menuPage = new MenuPage ();

            menuPage.Menu.ItemSelected += (sender, e) => NavigateTo (e.SelectedItem as MenuItem);

            Master = menuPage;
            Detail = new NavigationPage (new ContractsPage ());
        }

        void NavigateTo (MenuItem menu)
        {
            Page displayPage = (Page)Activator.CreateInstance (menu.TargetType);
            Detail =    new NavigationPage (displayPage);
            IsPresented = false;
        }
    }

那么有什么想法可以克服这个问题吗?

so any ideas how to overcome this problem?

推荐答案

就像@Sten-Petrov 所说的:您正在替换详细信息页面而不是触发历史记录机制.要触发历史记录机制,您需要在 Detail 页面的 Navigation 属性上执行 PushAsync(Page).

Like what @Sten-Petrov said: you are replacing the detail page and not triggering the history mechanism. To trigger the history mechanism you will need to do a PushAsync(Page) on the Navigation property of the Detail page.

在您的示例中,更改 NavigateTo:

In your example, change NavigateTo:

 void NavigateTo (MenuItem menu)
 {
     Page displayPage = (Page)Activator.CreateInstance (menu.TargetType);
     Detail.Navigation.PushAsync(displayPage);
 }

这不会替换内容,但会打开一个带有您想要的后退按钮功能的新页面.

This will not replace the content but will bring up a new page with the back button functionality you want.

如果您想要主从页面的后退按钮功能,那么您需要自定义后退堆栈过程,在我看来,这是不值得的.在这种情况下,只需移至不同的页面/导航结构即可.

If you want back button functionality with the Master-Detail page then you'll need to customize the back stack process which, in my opinion, is not worth it. Just move to a different page/navigation structure in that case.

这篇关于Xamarin.Forms - 主/详细页面和导航历史记录问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆