使用Xamarin Android动态创建菜单时出错

时间:2016-07-13 09:31:07

标签: c# xamarin.android

我需要在使用Xamarin Android开发的应用程序中动态创建菜单。 为此,我写了这段代码:

 public  class MyActivity : Activity
    {

        private static readonly int DRTC = Menu.First;
        private static readonly int DRR = Menu.First + 1;
           protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Create your application here
            SetContentView(Resource.Layout.activity_my);


        }
  public override bool OnCreateOptionsMenu(IMenu menu)
        {
            bool result = base.OnCreateOptionsMenu(menu);
            menu.Add(0, DRTC, 0, "Modify TC");
            menu.Add(0, DRR, 0, "Restart DR");

            return result;
        }
 public override bool OnOptionsItemSelected(IMenuItem item)
        {
            switch (item.ItemId)
            {
            case Resource.Id.DRTC: //ERROR
        //Code
         Break;
             case Resource.Id.DRR://ERROR
        //Code
             Break;
             bool result = base.OnOptionsItemSelected(item);
        return result;
}
}

错误表明Resource.Id不包含DRTC和DRR的定义。 你能不能看看我在这段代码中遗漏了什么。 谢谢

1 个答案:

答案 0 :(得分:1)

试试这个:

public  class MyActivity : Activity
    {

        private const int DRTC = 0;
        private const int DRR = 1;
           protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Create your application here
            SetContentView(Resource.Layout.activity_my);


        }
  public override bool OnCreateOptionsMenu(IMenu menu)
        {
            bool result = base.OnCreateOptionsMenu(menu);
            menu.Add(0, DRTC, 0, "Modify TC");
            menu.Add(0, DRR, 0, "Restart DR");

            return result;
        }
 public override bool OnOptionsItemSelected(IMenuItem item)
        {
            switch (item.ItemId)
            {
            case  DRTC: 
        //Code
         Break;
             case  DRR:
        //Code
             Break;
             bool result = base.OnOptionsItemSelected(item);
        return result;
}
}

看看here