XAMARIN.android - 获取id项目菜单

时间:2017-10-26 20:58:06

标签: android xamarin xamarin.android

我是xamarin的新手,实际上我正在使用Android菜单,但是我有一些问题需要获取项目ID并为每个项目启动活动:

MainActivity.cs

[Activity(Label = "sofis3", MainLauncher = true, Icon = "@mipmap/icon", Theme = "@style/Theme.DesignDemo")]
    public class MainActivity : AppCompatActivity
    {

        DrawerLayout drawerLayout;  
        NavigationView navigationView;  

        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.Main);

            var toolbar = FindViewById<V7Toolbar>(Resource.Id.toolbar);
            SetSupportActionBar(toolbar);
            SupportActionBar.SetDisplayHomeAsUpEnabled(true);
            SupportActionBar.SetDisplayShowTitleEnabled(false);
            SupportActionBar.SetHomeButtonEnabled(true);
            SupportActionBar.SetHomeAsUpIndicator(Resource.Mipmap.ic_menu);
            drawerLayout = FindViewById<DrawerLayout>(Resource.Id.drawer_layout);
            navigationView = FindViewById<NavigationView>(Resource.Id.nav_view);

        }

        public override bool OnOptionsItemSelected(IMenuItem item)   
        {  
            Intent intent = null;

            //Console.WriteLine("ID = " + item.ItemId);

            switch (item.ItemId)   
            {  
                case Android.Resource.Id.Home:  
                    drawerLayout.OpenDrawer(Android.Support.V4.View.GravityCompat.Start);  
                    break;

                case Android.Resource.chi_siamo :
                    intent = new Intent(this, typeof(ChiSiamoActivity));
                    break;

                case Android.Resource.Id.nostro_centro:
                    intent = new Intent(this, typeof(NostroCentroActivity));
                    break;

                case Android.Resource.Id.servizi_paziente:
                    intent = new Intent(this, typeof(ServiziPazienteActivity));
                    break;

                case Android.Resource.Id.my_sofis:
                    intent = new Intent(this, typeof(MySofisActivity));
                    break;

                case Android.Resource.Id.contatti:
                    intent = new Intent(this, typeof(ContattiActivity));
                    break;

                case Android.Resource.Id.lavora_con_noi:
                    intent = new Intent(this, typeof(LavoraConNoiActivity));
                    break;

                default://invalid option
                    return base.OnOptionsItemSelected(item);
            } 

            if (intent != null)
            {
                StartActivity(intent);
            }

            return base.OnOptionsItemSelected(item);  
        }

我有两个主要布局:

布局/ Main.axml
布局/菜单/ menu.xml文件

menu.xml文件

<?xml version="1.0" encoding="utf-8"?>  
<menu  
    xmlns:android="http://schemas.android.com/apk/res/android">  
    <group android:checkableBehavior="single">  
        <item  
            android:id="@+id/chi_siamo"  
            android:title="CHI SIAMO"
         />  
        <item  
            android:id="@+id/nostro_centro"  
            android:title="Il NOSTRO CENTRO" />  
        <item  
            android:id="@+id/servizi_paziente"  
            android:title="SERVIZI PER IL PAZIENTE" />  

        <item  
            android:id="@+id/my_sofis"  
            android:title="MY SOFIS" /> 

        <item  
            android:id="@+id/contatti"  
            android:title="CONTATTI" /> 

        <item  
            android:id="@+id/lavora_con_noi"  
            android:title="LAVORA CON NOI" /> 
    </group>  
</menu>   

问题是&#34;案例Android.Resource.chi_siamo&#34;没有获得ID,所以我无法开始特定的活动。

如何获取ID名称并进行验证? 谢谢你的帮助!

1 个答案:

答案 0 :(得分:1)

首先,您的menu.xml文件应位于以下目录中:

Resources -> menu -> menu.xml
Resources -> layout

创建菜单:

public override bool OnCreateOptionsMenu(IMenu menu)
{
    MenuInflater.Inflate(Resource.Menu.menu, menu);
    return base.OnCreateOptionsMenu(menu);
} 

然后移除Android并将Id添加到行:

case Android.Resource.chi_siamo :

像:

case Resource.Id.chi_siamo :

从所有其他Android案例中删除switch