MvxItemTemplate中的MvvmCross MvxSpinner

时间:2015-03-03 22:09:37

标签: c# android xamarin mvvmcross

我正在使用Xamarin和MvvmCross创建Android应用程序。我希望能够在MvxItemTemplate中使用MvxSpinner。我无法填充ItemsSource。 Item的DataContext是列表中的项而不是ViewModel。将整个建议列表添加到列表中的每个项目似乎效率低下。有没有办法将ItemsList与项目的datacontext分开填充?有没有办法将项目静态添加到axml视图?

...在整天工作之后,我发现如果我使用香草微调器,我可以通过xml属性设置项目列表:

android:entries="@array/TagColors"

如果我这样做,我无法绑定到结果,但是如果我使用MvxSpinner,我可以绑定到SelectedItem,但不能使用android:条目来填充我的列表。有谁知道这里有一个幸福的媒介?

1 个答案:

答案 0 :(得分:1)

好的,我是这样做的。这可能真的很脏,但它确实有效,所以我继续前进。我从名为ShipmentInventory的WCF服务中获取了一个对象。此Object包含ShipmentInventoryItem的ObservableCollection。这个可观察的集合是我绑定到ListView的东西:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Mvx.MvxListView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        local:MvxBind="ItemsSource ShipmentInventory.Items"
        local:MvxItemTemplate="@layout/inventoryitemview" />
    <FrameLayout
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/frameLayout2" />
    <EditText
        android:inputType="date"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/editText1" />
</LinearLayout>

这使用InventoryItemView作为MvxListView的模板,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <MvxSpinner
        android:layout_width="300dp"
        android:layout_height="match_parent"
        android:layout_margin="5dp"
        android:padding="2dp"
        local:MvxBind="ItemsSource TagColors; SelectedItem TagColor" 
        android:id="@+id/spinner1" />
    <EditText
        android:layout_width="300dip"
        android:layout_height="wrap_content"
        style="@style/InputEditText"
        local:MvxBind="Text InventoryNumber" />
    <EditText
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        style="@style/InputEditText"
        local:MvxBind="Text Articles" />
</LinearLayout>

所以诀窍是让ShipmentInventoryItem拥有一个包含颜色列表的TagColors属性。从WCF服务返回这一点的想法并不是很吸引人。然后我记得svcutil.exe创建了一堆部分类来创建每个对象。好吧,如果他们能做到,我也可以:)这是我添加到我的DataModel代码:

public partial class ShipmentInventoryItem
{
    private static string[] _TagColors = { "Yellow", "Brown", "White", "Blue", "Orange", "Red", "Green", "Purple" };
    public string[] TagColors
    {
        get { return _TagColors; }
    }
}

它像冠军一样工作。确保它位于WCF创建对象的命名空间中。由于数组被声明为静态,我很确定它只有一个实例。我是新手,所以如果有人看到我不这样做的原因,请告诉我。否则,我希望将来可以帮助某人。

相关问题