Xamarin- Android-导航视图的开头项目



Xamarin Android的新事物。不确定如何从下面的if语句中确切调用另一个视图/页面 - 我已经看到其他人对此使用片段或案例语句,但是我只是在学习,所以不要更改太多。这是带有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);
        }

以下是我要重新贴上的语句 - 我正在尝试理解如何在选择时从此处打开其他活动或查看。

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

您可以使用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
        ///...
        ///...
        ///...
    }
}

最新更新