简体   繁体   English

在Android中将属性绑定到mvxBindableListView

[英]Binding a Property to a mvxBindableListView in Android

I have a problem trying to bind a property of an object in a mvxBindableListView with MVVMCross and Android. 我在尝试使用MVVMCross和Android绑定mvxBindableListView中的对象的属性时遇到问题。

The structure of the classes is the next: 类的结构是下一个:

public class A 
{
    public string MyPropertyA1 { get; set; }
    public int MyPropertyA2 { get; set; }
}
public class B 
{
    public int MyPropertyB1 { get; set; }
    public int MyPropertyB2 { get; set; }
}

public class C
{
    public int MyPropertyC1 { get; set; }
    public A MyPropertyA { get; set; }
    public B MyPropertyB { get; set; }
}

The mvxBindableListView is like that: mvxBindableListView是这样的:

<?xml version="1.0" encoding="utf-8"?>
<Mvx.mvxBindableListView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res/Android.Client"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
local:MvxBind="{'ItemsSource':{'Path':'Results'}}"
local:MvxItemTemplate="@layout/detail_viewmodel" />

Where Results is: public ObservableCollection Results { get; 结果是:public ObservableCollection Results {get; set; 组; } and the detail_viewmodel is: ,而detail_viewmodel为:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res/Android.Client"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="6dip"
    android:layout_marginTop="6dip"
    android:textAppearance="?android:attr/textAppearanceLarge"
    local:MvxBind="{'Text':{'Path':'MyPropertyA.MyPropertyA1'}}" />

When the application runs, it shows me the mvxBindableListView with the number of elements that contains the variable 'Results' , but the rows are empty. 当应用程序运行时,它显示了mvxBindableListView以及包含变量'Results'的元素数量,但是行为空。

Anyone know what is happening? 有人知道发生了什么吗? thanks 谢谢

I tried changing the Tutorial application to use an email with: 我尝试将Tutorial应用程序更改为使用以下电子邮件:

    public class Thing
    {
        public string Life { get; set; }

        public Thing()
        {
            Life = Guid.NewGuid().ToString();
        }
    }

    public class SimpleEmail
    {
        public Thing MyThing { get; set; }
        public string From { get; set; }    
        public string Header { get; set; }    
        public string Message { get; set; }

        public SimpleEmail()
        {
            MyThing = new Thing();
        }
    }

And the template to include: 模板包括:

<TextView
android:id="@+id/email_header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="6dip"
android:layout_marginTop="6dip"
android:layout_toRightOf="@id/email_from"
local:MvxBind="{'Text':{'Path':'MyThing.Life'}}"
/>

This showed the Guid's fine. 这显示了Guid的罚款。

Looking at your code, because your models do not support INotifyPropertyChanged, then I wonder if you are setting the ViewModel properties before you are binding? 查看代码,因为您的模型不支持INotifyPropertyChanged,所以我想知道是否在绑定之前设置ViewModel属性?

To fix this, I might try: 要解决此问题,我可以尝试:

public class A 
{
    public string MyPropertyA1 { get; set; }
    public int MyPropertyA2 { get; set; }
}
public class B 
{
    public int MyPropertyB1 { get; set; }
    public int MyPropertyB2 { get; set; }
}

public class C
{
    public int MyPropertyC1 { get; set; }
    public A MyPropertyA { get; set; }
    public B MyPropertyB { get; set; }
    public C()
    {
       MyPropertyA = new A() { MyPropertyA1 = "TestValue" };
    }
}

If your app then shows "TestValue" then you know that the binding is working. 如果您的应用程序随后显示“ TestValue”,则说明绑定正在工作。

If you then need to get the right values shown, you have two choices: 如果随后需要获取正确的值,则有两种选择:

  1. Set the properties before you bind the view to the data 在将视图绑定到数据之前设置属性
  2. Somehow, somewhere on the binding chain signal the INotifyPropertyChange event so that the binding knows it has to refresh 以某种方式,绑定链上的某处发出信号通知INotifyPropertyChange事件,以便绑定知道它必须刷新

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM