将MVVM Light中的类绑定到Xamarin中的RelayCommand

时间:2015-02-13 21:38:12

标签: android xamarin mvvm-light

我正在尝试将以下类绑定到relaycommand。

  public class UserAuth
  {
    public string UserName { get; set; }
    public string Password { get; set; }
  }

这是我的MainActivity类:

public partial class MainActivity : ActivityBaseEx
{

     private Binding<string, UserAuth> _userInformation;


    private Binding<string, UserAuth> _cool;

    public LoginViewModel LoginViewModel
    {
        get
        {
            return App.Locator.Login;
        }
    }


    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        SetContentView(Resource.Layout.Main);



         _userInformation = this.SetBinding(()=> **....... WHAT GOES HERE!! I can do this for a simple string, but cannot figure it out for a class!**


        // Get our button from the layout resource and attach an event to it
        var signInButton = FindViewById<Button>(Resource.Id.btnSingIn);

        signInButton.SetCommand("Click", LoginViewModel.LoginCommand, _userInformation);

    }

}

这是我的视图模型中的RelayCommand

  public RelayCommand<UserAuth> LoginCommand
    {
        get
        {
            return _loginCommand ?? (_loginCommand = new RelayCommand<UserAuth>(
                async (userAuth) =>
                {

                    _isLoading = true;

                    try
                    {
                        //   var loggedIn = await _loginService.AuthenticateUser("emediaqa1", "p098765");

                        var loggedIn = await _loginService.AuthenticateUser(userAuth.UserName, userAuth.Password);

                        _isLoading = false;
                    }
                    catch (Exception ex)
                    {
                        var dialog = ServiceLocator.Current.GetInstance<IDialogService>();
                        dialog.ShowError(ex, "Error  Authenticating", "OK", null);
                    }

                    _isLoading = false;
                }));
        }
    }

我的问题在于这一行:

_userInformation = this.SetBinding(()=> // WHAT GOES HERE!! I can do  this for a simple 
                                        //string, but  cannot figure it out for a class! 

请帮忙!

谢谢!

1 个答案:

答案 0 :(得分:0)

我在MainActivity.OnCreate中使用了类似的东西:

_usernameBinding = this.SetBinding(() => Vm.userAuth.Username, () => Username.Text, BindingMode.TwoWay);
 _passwordBinding = this.SetBinding(() => Vm.userAuth.Password, () => Password.Text, BindingMode.TwoWay);

所以你需要2个绑定,一个用于userName,另一个用于Password。