本文介绍了MvvmCross-将“汉堡包"菜单更改为“后退"按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用XPlatformMenus示例Android项目,该项目位于 https://github.com/MvvmCross/MvvmCross-Samples/tree/master/XPlatformMenus

我想做的是,当用户导航到InfoFragment时,Hamburger菜单图标变为后退箭头,当按下应用程序时,导航至上一个视图/片段,在这种情况下为HomeFragment. /p>

我已经在MainActivity上看到了一个名为ShowBackButton的方法,但是没有从任何地方调用它,因此我将其代码添加到BaseFragment的OnCreateView中,在其中它检查ShowHamurgerMenu布尔值.我添加了一个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中.到目前为止,我设法隐藏了Hamburger菜单,但无法显示后退按钮.

我们将不胜感激任何建议或指导,甚至是对某些文章的参考.

非常感谢!

解决方案

通过以以下方式处理工具栏的NavigationClick事件来完成此工作:

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

I'm using the XPlatformMenus sample Android project available at https://github.com/MvvmCross/MvvmCross-Samples/tree/master/XPlatformMenus

What I want to do is, when the user navigates to the InfoFragment, that the Hamburger menu icon change to a back arrow and when pressed the app navigates to the previous view/fragment, which in this case is the HomeFragment.

I've seen a method called ShowBackButton on the MainActivity, but it is not called from anywhere, so I've added it's code to the BaseFragment's OnCreateView, where it checks the ShowHamurgerMenu bool. I've added an else, and the code looks something along these lines:

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

I set ShowHamburgerMenu = false; in the InfoFragment's OnCreateView. So far I managed to hide the Hamburger menu, but are not able to show a back button.

Any advice or guidance or even a reference to some articles that might help would be greatly appreciated.

Thank you very much!

解决方案

Got this working by handling the Toolbar's NavigationClick event in the following manner:

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

这篇关于MvvmCross-将“汉堡包"菜单更改为“后退"按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 00:28