UIBarButtonItem GetEvents returns 真实设备上有 0 个项目

UIBarButtonItem GetEvents returns 0 items on real device

我在 Xamarin.iOS 项目上使用 MVVM Light,我使用调用 Type.GetEvent() 的 SetCommand 将命令绑定到按钮。当我在模拟器上执行应用程序时一切正常,但是当我在真实设备(iOS 10.3.3)上执行应用程序时,SetCommand 方法失​​败并显示消息:“找不到事件:已单击 参数名称:eventName.

这只发生在 UIBarButtonItem 上,UIButtons 在两个环境中都使用 SetCommand 正确绑定到 "TouchUpInside"。

我已经测试了 _uiBarButton.GetType().GetEvents() 并在真实设备上返回了一个空数组。

这里会发生什么?有人可以帮忙吗?

好吧,在创建我的自定义扩展方法作为解决方法之后:

public static void SetBarButtonCommand(this UIBarButtonItem button, ICommand command)
{
  SetBarButtonCommand(button, string.Empty, command);
}

public static void SetBarButtonCommand(this UIBarButtonItem button, string eventName, ICommand command)
{
  button.Clicked += (s, args) =>
  {
    if (command.CanExecute(null))
    {
      command.Execute(null);
    }
  };

  button.Enabled = command.CanExecute(null);
  command.CanExecuteChanged += (s, args) =>
  {
    button.Enabled = command.CanExecute(null);
  };
}

问题解决了!!这很奇怪,因为在尝试之前我多次清理并重新编译了项目,但现在我再次使用默认的 MVVM Light SetCommand 扩展方法,并且在真实设备上一切正常,非常非常奇怪...

已编辑

我找到了一篇很好的文章来解释这个问题:

Solving the event not found