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