使用 MvvmCross
创建从 UIBarButtonItem
到 MvxCommand
的绑定(bind)的正确方法是什么?
这对我不起作用->
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
this.AddBindings (new Dictionary<object, string> (){
{MyButton, "{'Clicked':{'Path':'MyCommand'}}"}
});
}
我在控制台中没有收到任何错误。我可以通过避免绑定(bind)来解决问题,但你知道,如果所有内容都绑定(bind)的话看起来最好。
最佳答案
假设条形按钮 - MyButton - 在您调用 AddBindings 之前已初始化,那么我认为您的代码应该可以工作。
我将 Conference 示例中的 WelcomeView 中的代码修改为下面的代码,它似乎可以工作...
var s = new UIBarButtonItem()
{
Title = "Click me"
};
NavigationItem.SetRightBarButtonItem(s, false);
this.AddBindings(new Dictionary<object, string>()
{
{ Button1, "TouchUpInside ShowSponsorsCommand" },
{ Button2, "TouchUpInside ShowExhibitorsCommand" },
{ Button3, "TouchUpInside ShowMapCommand" },
{ Button4, "TouchUpInside ShowAboutCommand" },
{ s, "Clicked ShowMapCommand" },
});
//NavigationItem.SetRightBarButtonItem(new UIBarButtonItem("Tweet", UIBarButtonItemStyle.Bordered, (sender, e) => ViewModel.DoShareGeneral()), false);
很抱歉,如果这个“对我有用”的答案对您的机器没有帮助:/
为了确保它在 Release模式下工作,您可能需要在某处添加一些 UIBarButtonItem.Clicked
代码。
如果您希望从绑定(bind)中获得更多跟踪信息,那么您可以设置一个诊断跟踪级别(但这可能会变得非常冗长)。
可能值得检查您的 MyCommand
是否被调用但不知何故失败 - 这尤其可能发生在电子邮件、电话、推文等并不总是在模拟器上工作的事情上。
关于ios - 绑定(bind)到 MvvmCross 中的 UIBarButtonItem 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15200966/