OnOptionsItemSelected 在 Xamarin 中不起作用

Posted

技术标签:

【中文标题】OnOptionsItemSelected 在 Xamarin 中不起作用【英文标题】:OnOptionsItemSelected not working in Xamarin 【发布时间】:2015-11-04 05:53:30 【问题描述】:

我正在尝试使用 Xamarin 创建一个 android 应用程序。

我有一个带有左侧抽屉菜单的 ActionBarActivity 活动,其中包含很少的项目。但是当我按下其中一个项目时,没有任何反应,我在 OnOptionsItemSelected 函数内放置了一个断点,它并没有停在那里。这是我的 MainActivity.cs 文件:

namespace IBuy 
    [Activity(Label = "Main Screen", Theme="@style/MyTheme")]
    public class MainScreen: ActionBarActivity
    
        private SupportToolBar mToolbar;
        private MyActionBarDrawerToggle mDrawerToggle;
        private DrawerLayout mDrawerLayout;
        private ListView mLeftDrawer;


        protected override void OnCreate(Bundle bundle) 
            base.OnCreate (bundle);
            SetContentView(Resource.Layout.MainScreen);

            mToolbar = FindViewById<SupportToolBar> (Resource.Id.toolbar);
            mDrawerLayout = FindViewById<DrawerLayout> (Resource.Id.drawer_layout);
            mLeftDrawer = FindViewById<ListView> (Resource.Id.left_drawer);
            SetSupportActionBar (mToolbar);
            String[] arr = Resources.GetStringArray(Resource.Array.myList);

            ArrayAdapter<string> adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, arr);
            mLeftDrawer.Adapter = adapter;

            mDrawerToggle = new MyActionBarDrawerToggle (this, mDrawerLayout, Resource.String.openDrawer, Resource.String.closeDrawer);

            mDrawerLayout.SetDrawerListener (mDrawerToggle);

            SupportActionBar.SetHomeButtonEnabled (true);
            SupportActionBar.SetDisplayShowTitleEnabled (true);
            mDrawerToggle.SyncState ();
        

        public override bool OnOptionsItemSelected (IMenuItem item)
        
            Console.WriteLine ("ID = " + item.ItemId);
            mDrawerToggle.OnOptionsItemSelected (item);
            return base.OnOptionsItemSelected (item);
        

    

这是我的 MainScreen.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_
    android:layout_>
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_
        android:layout_
        android:minHeight="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Dark" />
    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_
        android:layout_>
        <RelativeLayout
            android:layout_
            android:layout_>
            .
            .
            .
        </RelativeLayout>
        <ListView
            android:id="@+id/left_drawer"
            android:layout_
            android:layout_
            android:layout_gravity="start"
            android:choiceMode="singleChoice"
            android:divider="#818181"
            android:dividerHeight="1dp"
            android:background="#E3F2FD" />
    </android.support.v4.widget.DrawerLayout>
</LinearLayout>

这是我的 MyActionBarDrawerToggle.cs 文件:

using System;
using SupportActionBarDrawerToggle = Android.Support.V7.App.ActionBarDrawerToggle;
using Android.Support.V7.App;
using Android.Support.V4.Widget;

namespace IBuy

    public class MyActionBarDrawerToggle : SupportActionBarDrawerToggle
    
        private ActionBarActivity mHostActivity;
        private int mOpenResource;
        private int mCloseResource;

        public MyActionBarDrawerToggle (ActionBarActivity host, DrawerLayout drawerLayout, int openedResource, int closedResource)
            : base(host,drawerLayout,openedResource,closedResource)
        
            mHostActivity = host;
            mOpenResource = openedResource;
            mCloseResource = closedResource;
        

        public override void OnDrawerOpened (Android.Views.View drawerView)
        
            base.OnDrawerOpened (drawerView);
        

        public override void OnDrawerClosed (Android.Views.View drawerView)
        
            base.OnDrawerClosed (drawerView);
        

        public override void OnDrawerSlide (Android.Views.View drawerView, float slideOffset)
        
            base.OnDrawerSlide (drawerView, slideOffset);
        

        public override bool OnOptionsItemSelected (Android.Views.IMenuItem item)
        
            int num = item.ItemId;
            return base.OnOptionsItemSelected (item);
        
    

我做错了什么?有人可以帮帮我吗?

提前谢谢你

【问题讨论】:

【参考方案1】:

不确定这个,但我认为您的问题在OnOptionsItemSelected

试着改成这个

switch(item.ItemId)

case Resource.Id.<controlID>: 
// run some code;
break;

controlID 是抽屉中声明的 ID

【讨论】:

这对我没有帮助,因为 OnOptionsItemSelected 甚至没有被调用。【参考方案2】:

答案完全不同。我刚刚处理了 ListView 上的 ItemClick 事件,现在它可以工作了。

【讨论】:

以上是关于OnOptionsItemSelected 在 Xamarin 中不起作用的主要内容,如果未能解决你的问题,请参考以下文章

在 Fragment 中添加 onOptionsItemSelected 调用

从 onOptionsItemSelected 访问列表视图

OnOptionsItemSelected 未通过使用 ScrollView 实现的方法调用

在Fragment上不调用onOptionsItemSelected()方法

菜单项onCreateOptionsMenu()和onOptionsItemSelected()的使用

AppCompat v7 工具栏 onOptionsItemSelected 未调用