关于c#:MvxListView,其中MvxSpinner在第一个项目上显示空条目

MvxListView with MvxSpinner showing null entry on the first Item

我正在使用一个包含MvxSpinner的MvxListView。当我的应用程序运行时,跟踪显示以下几个实例:

微调框的SelectedItem绑定中当前不允许使用空值

我知道数据对象上的条目不为空。以下是相关代码:MvxListView的布局为

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <MvxListView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                local:MvxBind="ItemsSource ShipmentLots.Lots"
                local:MvxItemTemplate="@layout/inventorylotview" />
            <ImageButton
                android:src="@drawable/ic_action_new"
                android:layout_width="60dp"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                local:MvxBind="Click NewLot_Clicked"
                android:id="@+id/btnLotNew" />
        </LinearLayout>

MvxItemTemplate的布局如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<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="130dp"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        style="@style/InputSpinner"
        local:MvxItemTemplate="@layout/itemspinner"
        local:MvxDropDownItemTemplate="@layout/itemspinnerdropdown"
        local:MvxBind="ItemsSource LotColors; SelectedItem LotColor"
        android:id="@+id/spinner1" />
    <EditText
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        style="@style/InputEditText"
        local:MvxBind="Text LotNo" />
    <ImageButton
        android:src="@drawable/ic_action_delete"
        android:layout_width="60dp"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        local:MvxBind="Click DeleteClicked"
        android:id="@+id/btnLotDelete" />
</LinearLayout>

InventoryViewModel如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
public class InventoryViewModel
  : MvxViewModel
{
    public async void Init(Guid ID)
    {
        await MPS_Mobile_Driver.Droid.DataModel.ShipmentDataSource.GetShipmentInventory(ID);
        ShipmentInventory = ShipmentDataSource.CurrInventory;

        Shipment = await MPS_Mobile_Driver.Droid.DataModel.ShipmentDataSource.GetShipment((int)ShipmentInventory.idno, (short)ShipmentInventory.idsub);
        ShipmentLots = await MPS_Mobile_Driver.Droid.DataModel.ShipmentDataSource.GetShipmentLotList(Shipment.idno, Shipment.idsub);
    }

    private Shipment _Shipment;
    public Shipment Shipment
    {
        get { return _Shipment; }
        set { _Shipment = value; RaisePropertyChanged(() => Shipment); }
    }

    private ShipmentInventory _ShipmentInventory;
    public ShipmentInventory ShipmentInventory
    {
        get { return _ShipmentInventory; }
        set { _ShipmentInventory = value; RaisePropertyChanged(() => ShipmentInventory); }
    }

    private ShipmentLotList _ShipmentLots;
    public ShipmentLotList ShipmentLots
    {
        get { return _ShipmentLots; }
        set { _ShipmentLots = value; RaisePropertyChanged(() => ShipmentLots); }
    }

    public IMvxCommand NewLot_Clicked
    {
        get
        {
            return new MvxCommand(() => NewLot());
        }
    }

    private void NewLot()
    {
        ShipmentLot Lot = new ShipmentLot();
        Lot.ID = Guid.NewGuid();
        Lot.idno = Shipment.idno;
        Lot.idsub = Shipment.idsub;
        ShipmentLots.Lots.Add(Lot);
    }

}

ShipmentLots的视图模型包含一个称为Lots的ShipmentLot类型的可观察集合。从WCF服务创建ShipmentLots的类。我将其扩展如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public partial class ShipmentLot
{

    private static string[] _LotColors = {"Yellow","Brown","White","Blue","Orange","Red","Green","Purple" };
    public string[] LotColors
    {
        get { return _LotColors; }
    }

    public IMvxCommand DeleteClicked
    {
        get
        {
            return new MvxCommand(() => DeleteLot());
        }
    }

    private void DeleteLot()
    {
        MPS_Mobile_Driver.Droid.Views.InventoryView act = (MiscFunctions.CurrActivity as MPS_Mobile_Driver.Droid.Views.InventoryView) ?? null;
        if (act != null)
        {
            act.DeleteLot(this);
        }
    }
}

这需要使删除按钮起作用并为MvxSpinner提供颜色列表。运行应用程序时,出现Null值不允许错误,并且MvxListView中的第一项在MvxSpinner上具有错误的颜色。随后的那些可以正常工作。我不确定第一个有什么不同。有人对此有任何想法吗?

谢谢,
吉姆


在@Cheesebaron和@Stuart的大力帮助下,我发现,如果您在MvxItemList内的ItemTemplate中使用MvxSpinner或MvxAutoComplete,则以上层次结构中(包括MvxItemlist)的所有内容都不能包含android:layout_height =" wrap_content"。原因是,如果要动态确定其高度,Android OS必须绘制多个对象。所有重绘都会使绑定中的内容混乱。如果将所有内容设置为固定高度,则一切正常。要解决以上问题,上述MvxItemView的标记应为

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical">
        <MvxListView
            android:layout_width="fill_parent"
            android:layout_height="300dp"
            local:MvxBind="ItemsSource ShipmentLots.Lots"
            local:MvxItemTemplate="@layout/inventorylotview" />
        <ImageButton
            android:src="@drawable/ic_action_new"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:layout_gravity="center"
            local:MvxBind="Click NewLot_Clicked"
            android:id="@+id/btnLotNew" />
    </LinearLayout>

关键似乎在于安排标记,因此不必预先渲染MvxItemList即可确定屏幕区域的高度。如果您想了解更多信息,可以参考以下内容:

https://github.com/MvvmCross/MvvmCross/issues/944

我还有一个工作示例,说明如何在MvxItemList内执行MvxSpinner:

https://github.com/JimWilcox3/MvxSpinnerTest

此代码作为回购开始以演示该错误。一旦@Cheesebaron向我展示了错误的地方,我就对其进行了更正,因此这是一个有效的示例。希望这会对某人有所帮助。