我尝试创建MvxTabsFragmentActivity
并将片段上的按钮绑定到命令。我遇到的问题是以下错误。
MvxBind:Warning: 7.94 Failed to create target binding for binding Command for SignInCommand
[0:] MvxBind:Warning: 7.94 Failed to create target binding for binding Command for SignInCommand
04-01 00:40:53.062 I/mono-stdout( 5079): MvxBind:Warning: 7.94 Failed to create target binding for binding Command for SignInCommand
04-01 00:40:53.082 D/Mono ( 5079): Remapped public key token of retargetable assembly System from 7cec85d7bea7798e to b77a5c561934e089
04-01 00:40:53.082 D/Mono ( 5079): The request to load the retargetable assembly System v2.0.5.0 was remapped to System v2.0.0.0
04-01 00:40:53.092 D/Mono ( 5079): Unloading image System.dll [0x7f041ff0].
04-01 00:40:53.092 D/Mono ( 5079): Image addref System[0x7f04b030] -> System.dll[0x79befc10]: 16
[0:]
04-01 00:40:53.092 D/Mono ( 5079): Assembly Ref addref Cirrious.CrossCore[0x753c1000] -> System[0x79be85c0]: 15
MvxBind:Warning: 7.99 Failed to create target binding for binding CommandParameter for .
[0:] MvxBind:Warning: 7.99 Failed to create target binding for binding CommandParameter for .
04-01 00:40:53.112 I/mono-stdout( 5079): MvxBind:Warning: 7.99 Failed to create target binding for binding CommandParameter for .
我的片段绘制得恰到好处,但MvxBind没有绑定。
这里是axml
<Button
android:id="@+id/RegisterFragment_SignInButton"
android:layout_below="@id/SignInFragment_Password"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/signin_text"
local:MvxBind="Command SignInCommand; CommandParameter ." />
这是我的活动
public class AuthView : MvxTabsFragmentActivity
{
private new AuthViewModel ViewModel { get { return (AuthViewModel)base.ViewModel; } }
public AuthView() : base(Resource.Layout.AuthView, Resource.Id.actualtabcontent)
{
}
protected override void AddTabs(Bundle args)
{
AddTab<SignInFragment>("Login", GetString(Resource.String.signin_text), args, ViewModel.SignInViewModel);
}
}
从那里,我有一个片段
public class SignInFragment : MvxFragment
{
public override View OnCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
var ignored = base.OnCreateView(inflater, container, savedInstanceState);
return this.BindingInflate(Resource.Layout.SignInFragment, null);
}
}
和两个ViewModels
public class AuthViewModel : MvxViewModel
{
public AuthViewModel()
{
SignInViewModel = Mvx.IocConstruct<SignInViewModel>();
RegisterViewModel = Mvx.IocConstruct<RegisterViewModel>();
}
public MvxViewModel SignInViewModel { get; set; }
public MvxViewModel RegisterViewModel { get; set; }
}
public class SignInViewModel : MvxViewModel
{
public SignInViewModel(ISignInCommand signInCommand)
{
_signInCommand = signInCommand;
}
private string _email;
public string Email
{
get { return _email; }
set { _email = value; RaisePropertyChanged(() => Email); }
}
private string _password;
public string Password
{
get { return _password; }
set { _password = value; RaisePropertyChanged(() => Password); }
}
private ISignInCommand _signInCommand;
public ISignInCommand SignInCommand
{
get { return _signInCommand; }
set { _signInCommand = value; RaisePropertyChanged(() => SignInCommand); }
}
public void ShowViewModel<T>() where T : IMvxViewModel
{
base.ShowViewModel<T>();
}
}
我在哪里可能会出错?
答案 0 :(得分:1)
我认为这与Fragment
s
Android中的标准Button
没有Command
属性 - 他们有Click
个事件。
它们也没有CommandParameter
属性 - 但是MvvmCross提供了一个CommandParameter
转换器,允许您进行如下绑定:
local:MvxBind="Click CommandParameter(VMCommandName, VMCommandParameterName)"
如果您想将Command
和CommandParameter
添加到MyButton
,这可以很容易地完成:
public class MyButton : Button {
public MyButton(Context c, IAttributeSet a) : base (c, a) {
Click += (s,e) => {
if (Command == null) return;
if (!Command.CanExecute(CommandParameter)) return;
Command.Execute(CommandParameter);
}
}
public ICommand Command {get;set;}
public object CommandParameter {get;set;}
}
在axml中用作:
<MyButton local:MvxBind="Command VMCommandName; CommandParameter VMCommandParameterName" />