MvvmCross - 更改汉堡菜单备份按钮(MvvmCross - Change Hamburg

2019-09-27 17:39发布

我使用的可用XPlatformMenus样品Android项目在https://github.com/MvvmCross/MvvmCross-Samples/tree/master/XPlatformMenus

我想要做的是,当用户导航到InfoFragment,该汉堡菜单图标的变化来一回箭头,按下时,应用程序导航到一个视图/片段,在这种情况下是HomeFragment。

我见过一个叫ShowBackButton上在MainActivity的方法,但它不是从任何地方调用,所以我已经添加了它的代码到BaseFragment的OnCreateView,它会检查ShowHamurgerMenu布尔。 我已经添加了其他的,并且代码看起来这些方针的东西:

                if (ShowHamburgerMenu)
            {
                mainActivity.SupportActionBar?.SetDisplayHomeAsUpEnabled(true);

                DrawerToggle = new MvxActionBarDrawerToggle(
                    Activity, // host Activity
                    mainActivity.DrawerLayout, // DrawerLayout object
                    Toolbar, // nav drawer icon to replace 'Up' caret
                    Resource.String.drawer_open, // "open drawer" description
                    Resource.String.drawer_close // "close drawer" description
                );

                DrawerToggle.DrawerOpened += (sender, e) => mainActivity?.HideSoftKeyboard();
                mainActivity.DrawerLayout.AddDrawerListener(DrawerToggle);
            }
            else
            {
                mainActivity.SupportActionBar?.SetDisplayHomeAsUpEnabled(false);

                DrawerToggle = new MvxActionBarDrawerToggle(
                    Activity, // host Activity
                    mainActivity.DrawerLayout, // DrawerLayout object
                    Toolbar, // nav drawer icon to replace 'Up' caret
                    Resource.String.drawer_open, // "open drawer" description
                    Resource.String.drawer_close // "close drawer" description
                );

                DrawerToggle.DrawerIndicatorEnabled = false;
                //mainActivity.DrawerLayout.SetDrawerLockMode(DrawerLayout.LockModeLockedClosed);

                //mainActivity.SupportActionBar?.SetDisplayHomeAsUpEnabled(false);
                //mainActivity.ShowBackButton();
            }

我设置ShowHamburgerMenu = FALSE; 在InfoFragment的OnCreateView。 到目前为止,我设法隐藏汉堡包菜单,但无法显示后退按钮。

任何意见或指导甚至引用一些文章可能有助于将不胜感激。

非常感谢你!

Answer 1:

通过处理以下方式工具栏的NavigationClick事件得到了这个工作:

    private void Toolbar_NavigationClick(object sender, Toolbar.NavigationClickEventArgs e)
    {
        var mainActivity = Activity as MainActivity;
        mainActivity?.OnBackPressed();
    }


文章来源: MvvmCross - Change Hamburger menu to back button