MvvmCross - 将汉堡菜单更改为后退按钮

时间:2017-01-10 08:40:21

标签: android xamarin xamarin.android mvvmcross

我正在https://github.com/MvvmCross/MvvmCross-Samples/tree/master/XPlatformMenus

使用XPlatformMenus示例Android项目

我想要做的是,当用户导航到InfoFragment时,汉堡菜单图标变为后退箭头,当按下时,应用程序导航到上一个视图/片段,在本例中是HomeFragment。 / p>

我在MainActivity上看到了一个名为ShowBackButton的方法,但是它并没有从任何地方调用,所以我把它的代码添加到了BaseFragment的OnCreateView,它检查了ShowHamurgerMenu bool。我添加了一个else,代码看起来就像这些:

                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中。到目前为止,我设法隐藏汉堡菜单,但无法显示后退按钮。

任何建议或指导,甚至对某些可能有用的文章的引用都将不胜感激。

非常感谢!

1 个答案:

答案 0 :(得分:1)

通过以下方式处理工具栏的NavigationClick事件来实现此目的:

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