0
目前有一个恼人的问题,我的UIButton没有被绑定到ViewModel。当点击Button时,ViewModel的IMVXCommand不会被调用。该按钮显示,但不识别触摸事件。真正奇怪的是,如果我使用Interface Builder创建LoginButton,则下面的代码有效。MVVMCross:UIButton没有绑定(编程)
下面是示例代码:
[Register("LoginView")]
public class LoginView : MvxView
{
UIBUtton loginButton;
public LoginView(IntPtr handler) : base(handler)
{
}
public LoginView(CGRect frame) : base(frame)
{
this.Frame = UIScreen.MainScreen.ApplicationFrame;
init();
}
init()
{
CreateView();
}
CreateView()
{
loginButton = new UIButton(new CoreGraphics.CGRect(0, 193, 300, 30))
{
Font = UIFont.SystemFontOfSize(15, UIFontWeight.Medium),
TranslatesAutoresizingMaskIntoConstraints = false,
BackgroundColor = UIColor.Red,
};
AddSubView(loginButton);
var set = this.CreateBindingSet<LoginView, LoginViewModel>();
set.Bind(loginButton).To(vm => vm.ClickCommand);
set.Apply();
}
}
视图模型:
public class LoginViewModel : MvxViewModel
{
public AuthenticationViewModel() : base()
{
}
public IMvxCommand ClickCommand
{
get
{
return new MvxCommand(HandleClick);
}
}
public void HandleClick()
{
//This never gets called
}
}
我认为正确的解决方案是将容器MvxViewController的Datacontext传递给LoginView。这样两个视图都将具有相同的LoginViewModel实例 – Summon