【问题标题】:MVVMCross Data Binding not workingMVVMCross 数据绑定不起作用
【发布时间】:2017-07-18 14:02:21
【问题描述】:

我的 ViewModel 中有自定义对象。

public MyObject PropertyName
{
    get { return _propertyName; }
    set
    {
        _propertyName = value;
        RaisePropertyChanged(() => PropertyName);
    }
}

我只想将其成员之一绑定到我的机器人视图中的 textview。找到这个链接: Bind data of custom object to TextView in MvvmCross 我正在这样做。 local:MvxBind = "Text Myobject.ItsMember"

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:text="LabelCaption" />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:textColor="@android:color/white"
                    local:MvxBind="Text Myobject.ItsMember" />
            </RelativeLayout>
        </LinearLayout>
    </ScrollView>
</LinearLayout>

检查了大小写等,但绑定不起作用,我看不到屏幕上出现的字符串值,而我可以在调试窗口中打印它。 任何线索我可能会出错?

【问题讨论】:

  • 你在哪里夸大这个视图?
  • [Activity(Label = "MyView")] public class MyView : MvxActivity { protected override void OnViewModelSet() { SetContentView(Resource.Layout.MyView); } }
  • 而Myobject 是您的 ViewModel 中的属性名称?
  • MyObject 是类型。我使用属性名称绑定它 - 像这样 - local:MvxBind="Text PropertyName.Member"
  • 您不能在 MyObject 中绑定成员变量,它们必须是属性。

标签: xamarin xamarin.android mvvmcross


【解决方案1】:

我无法重现您的问题。

我试过了:

public class MyViewModel : MvxViewModel
{
    private MyModel _model; // this is a class member variable or class member field
    public MyModel Model // this is a class member property
    {   
        get => _model;
        set => SetProperty(ref _model, value);
    }

    public MyViewModel()
    {
        Model = new MyModel { Name = "Herp" };
    }
}

public class MyModel : MvxNotifyPropertyChanged
{
    private string _name;
    public string Name
    {
        get => _name;
        set => SetProperty(ref _name, value);
    }
}

那么鉴于MyViewModel 是我绑定的DataContext:

local:MvxBind="Text Model.Name"

工作得很好。

【讨论】:

    【解决方案2】:

    您不能绑定到类型。您需要绑定到该类型的实例。为了使绑定按照您在 AXML 中设置的方式工作,您需要类似

    public class MyType
    {
        private string _ItsMember;
        public string ItsMember
        {
            get
            {
                return _ItsMember;
            }
            set
            {
                _ItsMember = value;
                RaisePropertyChanged (()=>ItsMember);
            }
        }
    }
    
    private MyType _MyObject;
    public MyType MyObject
    {
        get
        {
            return _MyObject;
        }
        set
        {
            _MyObject = value;
            RaisePropertyChanged(()=>MyObject);
        }
    }
    

    这种安排将允许 MyObject.ItsMember 解析为字符串属性值。如果您的属性不是字符串值,则必须实现一个转换器,它是一个完整的“另一个主题”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-23
      • 1970-01-01
      • 1970-01-01
      • 2017-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多