如何知道导航抽屉是否在Xamarin中打开?

时间:2017-11-24 00:17:00

标签: android xamarin xamarin.android

我的Main上有一个NavigationDrawer,当我按下Back按钮(打开NavigationDrawer时)应用程序退出,我希望它只关闭NavigationDrawer。在Java中,Android有这个:

@Override
public void onBackPressed()
{
  if (DrawerLayout.isDrawerOpen(Gravity.START))
    DrawerLayout.closeDrawer(Gravity.START);
  else
    super.onBackPressed();
}

如何在Xamarin.Android中制作?

2 个答案:

答案 0 :(得分:1)

试试这段代码:

public override void OnBackPressed()
{
    //if (drawerLayout.IsDrawerOpen(YourNavigationView))
    if (drawerLayout.IsDrawerOpen(Android.Support.V4.View.GravityCompat.Start))
    {
        drawerLayout.CloseDrawers();
        // or
        //drawerLayout.CloseDrawer(Android.Support.V4.View.GravityCompat.Start);
        //drawerLayout.CloseDrawer(navigationView);
    }
    else
        base.OnBackPressed();
}

答案 1 :(得分:0)

只需委托这样的方法:

drawerLayout = FindViewById<DrawerLayout>(Resource.Id.drawerLayoutID);
drawerLayout.DrawerOpened += delegate
{
   //code when drawer is opened!
};