Xamarin - Android - 从导航视图打开项目?

时间:2018-04-27 09:01:01

标签: c# android xamarin.android navigation-drawer

Xamarin Android的新手。不确定如何从下面的if语句调用另一个视图/页面 - 我已经看到其他人使用片段或case语句,但我只是学习所以不想改变太多。这是VS17的基本导航抽屉模板。

protected override void OnCreate(Bundle savedInstanceState)
        {

            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.activity_main);
            Android.Support.V7.Widget.Toolbar toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
            SetSupportActionBar(toolbar);

            FloatingActionButton fab = FindViewById<FloatingActionButton>(Resource.Id.fab);
            fab.Click += FabOnClick;

            DrawerLayout drawer = FindViewById<DrawerLayout>(Resource.Id.drawer_layout);
            ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, Resource.String.navigation_drawer_open, Resource.String.navigation_drawer_close);
            drawer.AddDrawerListener(toggle);
            toggle.SyncState();

            NavigationView navigationView = FindViewById<NavigationView>(Resource.Id.nav_view);
            navigationView.SetNavigationItemSelectedListener(this);
        }

以下是我正在准备的if语句 - 我正在尝试了解如何在选择时从此处打开另一个活动或视图。

public bool OnNavigationItemSelected(IMenuItem item)
    {

        int id = item.ItemId;

        if (id == Resource.Id.nav_support)
        {

        }

        else if (id == Resource.Id.nav_housing)
        {

        }
        else if (id == Resource.Id.nav_council)
        {

        }
        else if (id == Resource.Id.nav_education)
        {

        }
        else if (id == Resource.Id.nav_employment)
        {

        }
        else if (id == Resource.Id.nav_transport)
        {

        }
        else if (id == Resource.Id.nav_policing)
        {


        }
        else if (id == Resource.Id.nav_fire)
        {

        }
        else if (id == Resource.Id.nav_medical)
        {

        }
        DrawerLayout drawer = FindViewById<DrawerLayout>(Resource.Id.drawer_layout);
        drawer.CloseDrawer(GravityCompat.Start);
        return true;
    }
}

1 个答案:

答案 0 :(得分:0)

您可以使用intent在另一项活动中打开另一项活动 例如:

public class MyNavigationItemSelectedListener : Java.Lang.Object, NavigationView.IOnNavigationItemSelectedListener
{
    Context context;
    public MyNavigationItemSelectedListener(Context context)
    {
        this.context = context;
    }


    bool NavigationView.IOnNavigationItemSelectedListener.OnNavigationItemSelected(IMenuItem item)
    {
        int id = item.ItemId;

        if (id == Resource.Id.nav_support)
        {
            Intent intent = new Intent(context, typeof(SupportActivity));  //the activity you want to open
            context.StartActivity(intent);

        }
        ///Other code
        ///...
        ///...
        ///...

    }
}