Question

I'm trying to create an MvxTabsFragmentActivity and bind a button on a fragment to a command. The problem I'm getting is the following error.

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 .

My fragments are drawn exactly right, but the MvxBind isn't binding.

Here's the 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 ." />

Here's my Activity

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);
    }
}

From there, I have a Fragment

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);
    }
}

and two 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>();
    }
}

Where might I be going wrong here?

Was it helpful?

Solution

I don't think this has anything to do with Fragments

Standard Buttons in Android don't have Command properties - they have Click events.

They also don't have CommandParameter properties - but MvvmCross provides a CommandParameter converter to allow you to do bindings like:

local:MvxBind="Click CommandParameter(VMCommandName, VMCommandParameterName)"

If you want to add Command and CommandParameter to a MyButton this can be done quite easily:

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;}
}

Used in axml as:

<MyButton local:MvxBind="Command VMCommandName; CommandParameter VMCommandParameterName" />
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top