如何绑定到菜单项单击事件

如何绑定到菜单项单击事件

本文介绍了MvvmCross for MonoDroid-如何绑定到菜单项单击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MvvmCross PCL和一个相关的MonoDroid UI项目(该模式是从Stuart Lodge出色的N + 1视频系列中复制的).我想将操作栏菜单项的单击绑定到ViewModel中的ICommand.我看过这篇文章,但是IMenuItem却没有似乎没有合适的属性可以绑定.我知道SetOnMenuItemClickListener方法,但是(毫无疑问,这是我缺乏理解)不知道如何利用此方法将System.Windows.Input.ICommand绑定到该方法.

I have an MvvmCross PCL and a related MonoDroid UI project (pattern copied from following Stuart Lodge's excellent N+1 video series). I want to bind the click of an Action Bar menu item to an ICommand in my ViewModel. I've seen this post but an IMenuItem doesn't appear to have a suitable property to bind to. I'm aware of the SetOnMenuItemClickListener method but (and this is no doubt my lack of understanding) don't know how to leverage this to bind a System.Windows.Input.ICommand to it.

如果可以的话,是否还可以在MvxView的OnCreateOptionsMenu方法中完成操作,因为这为操作栏菜单本身提供了一个句柄?

Also, if this is possible, can it be done in the OnCreateOptionsMenu method of the MvxView as this provides a handle to the Action Bar menu itself?

推荐答案

我不认为您可以绑定它,因为ActionBar是在XML视图之外加载的.而且,由于从菜单xml文件中填充了项目,因此它不会公开任何C#类型的事件来进行单击.但是,单击MenuItem时,您可以在SetOnMenuItemClickListener中执行命令.您可以简单地执行以下命令:

I don't think you can bind it, as the ActionBar is something that is loaded outside of the XML view. Also it does not expose any C# type of events for clicks on items as they are populated from a menu xml file. However you could in your SetOnMenuItemClickListener execute commands when a MenuItem is clicked. You can simply execute a command like this:

ViewModel.MyAwesomeCommand.Execute();

这篇关于MvvmCross for MonoDroid-如何绑定到菜单项单击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 13:17