MvvmCross 绑定 Viewmodel 属性 到视图 属性

MvvmCross bind Viewmodel property to view property

是否可以从视图中更改视图模型 属性?我曾尝试过流畅的绑定,但 viewmodel 属性 始终为 null

查看

    public class UserProfileView : MvxActivity
{

    private string _currentToken;
    public string CurrentToken { get; set; }
    protected override void OnCreate(Bundle bundle)
    {
        var accounts = AccountStore.Create(this).FindAccountsForService("Soundcloud").ToList();
        var set = this.CreateBindingSet<UserProfileView, UserProfileViewModel>();
        set.Bind(this).For(v => v.CurrentToken).To(vm => vm.UserToken).TwoWay();
        set.Apply();
        accounts.ForEach(account =>
        {
            CurrentToken = account.Properties["access_token"];
        });
        base.OnCreate(bundle);
        SetContentView(Resource.Layout.UsrProfile);
    }
}

模型视图

    private string _userToken;
    public string UserToken
    {
        get { return _userToken;}
        set { _userToken = value; RaisePropertyChanged("UserToken"); Update();}
    }

最简单的方法是从 MvxActivity<TViewModel>

继承
public class UserProfileView : MvxActivity<UserProfileViewModel>

然后简单地设置

ViewModel.CurrentToken = account.Properties["access_token"];

这回答了您的 "Is it possible to change viewmodel property from view?"。但这不使用数据绑定。如果你真的想使用数据绑定,你必须为它编写一个自定义绑定,在这种情况下可能会花费太多精力。