When in a NSMenuItem the action is defined in code using the c# event style the function validateMenuItem is never called, however if the action is defined with a selector validateMenuItem is called as expected.
menuItem1.Activated+= (sender, e) =>
menuItem2.Action = new ObjCRuntime.Selector("menuItem2Clicked");
public void menuItem2Clicked(NSObject sender)
public bool validateMenuItem(NSMenuItem menuItem)
//never called for menuItem1, only for menuItem2
The expected behavior is validateMenuItem to be called for both menu items but it seems that the native code can not find where is defined the validateMenuItem function when the c# style for the menu item action was used.
I can confirm that this is indeed a bug. Looks like the class created in the delegate by the runtime does not have the validateMenuItem: selector and it is set as the target of the NSMenuItem, I will attach a minimal example of an application showing the issue.
PS: You forgot the colon in the selector. Should have been menuItem2.Action = new ObjCRuntime.Selector("menuItem2Clicked:");
Created attachment 19234 [details]
Example on an application showing the issue.
Not a great app but shows the issue.
I believe the issue might be:
Our ActionDispatcher likely doesn't have the menu selector, and it at first glance appears this API calls it on your target.
This might be painful to fix.
Until then, use the action/target "raw" APIs.
Hi, I just discovered this bug report after posting this on SO - https://stackoverflow.com/questions/46535458/getting-validatemenuitem-to-fire-for-nsmenuitem-using-xamarin-generated-action
In my case I am using MvvmCross to generate my menu items in my .Core cross-platform backend, and automatically generate the NSMenu. However, using this method validateMenuItems is not called. In my case I cannot directly use [Action("..")]. Any tips would be much appreciated as I am going batty looking at this.
Is there a clean way I can generate [Action("...")] at runtime for my class?
Chris, are you saying that the ActionDistpatcher needs a validateMenuItems: and that is where it would be implemented?
I somehow want to export the action at runtime
foreach (var menuItem in ViewModel.ContextMenu)
var action = new ActionAttribute("lookupAction:");
action.Action = menuItem.Command.ExecuteAsync();
var nsMenuItem = new NSMenuItem(menuItem.Title, new ObjCRuntime.Selector(action.Selector), "");
but I can't. Any tips or ideas much appreciated.
Just wanted to report that after I gave up on (1) using the NSMenuItem event handler and (2) making an action for every menuItem, I was able to make headway with a workaround. Use a single pre-registered action for all the menuItems, assign the NSMenuItem representedObject with a NSObject wrapped object with my commands.
Hello, this is indeed a very hard bug to fix due to the way we generate the bindings. I did point this out to the Mac OS X developers, will bring it up to them again to see if they can provide you with a nice workaround or at least confirm when will the bug be tackled.