自定义菜单项单击后如何关闭Android菜单?

时间:2018-12-25 06:28:12

标签: c# android xamarin.forms

单击Xamarin Forms Android自定义菜单项后出现问题。

在我的MainActivity类中,我有:

public override void OnActionModeStarted(ActionMode mode)
{
    IMenu menu = mode.Menu;
    menu.Add("To Notes");
    menu.GetItem(0).SetOnMenuItemClickListener(new MyMenuItemOnMenuItemClickListener(this));

    base.OnActionModeStarted(mode);
}

我的处理程序如下:

public class MyMenuItemOnMenuItemClickListener : Java.Lang.Object, IMenuItemOnMenuItemClickListener
{
    private MainActivity mContext;
    public Func<string, Task<string>> EvaluateJavascript { get; set; }

    public MyMenuItemOnMenuItemClickListener(MainActivity activity)
    {
        this.mContext = activity;
    }

    public bool OnMenuItemClick(IMenuItem item)
    {
        //Web is a static class declared elsewhere
        Web.CopyToMainNotes();

        return true;
    }
}

单击“ To Notes”菜单项时,无论OnMenuItemClick处理程序中的菜单如何,菜单都不会消失。

有什么想法吗?预先感谢。

1 个答案:

答案 0 :(得分:0)

尝试以下代码:(使用ActionMode.finish()

public override void OnActionModeStarted(ActionMode mode)
{
    IMenu menu = mode.Menu;
    menu.Add("To Notes");
    menu.GetItem(0).SetOnMenuItemClickListener(new MyMenuItemOnMenuItemClickListener(this,mode));

    base.OnActionModeStarted(mode);
}

处理程序

public class MyMenuItemOnMenuItemClickListener : Java.Lang.Object, IMenuItemOnMenuItemClickListener
{
    private MainActivity mContext;
    public Func<string, Task<string>> EvaluateJavascript { get; set; }
    private ActionMode actionMode;

    public MyMenuItemOnMenuItemClickListener(MainActivity activity,ActionMode mode)
    {
        this.mContext = activity;
        this.actionMode=mode;
    }

    public bool OnMenuItemClick(IMenuItem item)
    {
        //Web is a static class declared elsewhere
        Web.CopyToMainNotes();
        //close menu if menu item is clicked
        if(actionMode!=null){
        actionMode.finish()
        }
        return true;
    }
}