0
votes

I am creating and Android App using Xamarin and MvvmCross. I want to be able to use an MvxSpinner in a MvxItemTemplate. I am having trouble getting the ItemsSource to populate. The DataContext of the Item is the item in the list rather than the ViewModel. It seems inefficient to add the entire list of suggestions to each Item in the list. Is there a way to populate the ItemsList separate from the datacontext of the item? Is there a way to just add the items statically to the axml view?

...After working on this all day, I have discovered that if I use a vanilla spinner, I can set the item list via the xml attribute:

android:entries="@array/TagColors"

If I do this, I can't bind to the result, however if I use a MvxSpinner, I can bind to SelectedItem, but cannot use the android:entries to populate my list. Does anyone know of a happy medium here?

1

1 Answers

1
votes

OK here is how I did this. It is probably really dirty, but it works so I am moving on. I was getting an object from a WCF service called ShipmentInventory. This Object contained an ObservableCollection of ShipmentInventoryItem. This observable collection is what I was binding to the 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>

This uses InventoryItemView as the template for the MvxListView which is as follows:

<?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>

So the trick was having the ShipmentInventoryItem to have a TagColors property which contains a list of colors. The thought of returning this from the WCF service wasn't very appealing. Then I remembered that svcutil.exe creates a bunch of partial classes to create each object. Well if they can do it, so can I :) Here is what I added to my DataModel code:

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

It worked like a champ. Just be sure it is in the namespace where WCF created your objects. Since the array is declared static, I am pretty sure there is only ever one instance of it. I am new to this so if anyone sees a reason for me not to do it this way, please let me know. Otherwise, I hope this helps someone in the future.