【问题标题】:MvxListView with MvxSpinner showing null entry on the first ItemMvxListView 和 MvxSpinner 在第一个项目上显示空条目
【发布时间】:2026-01-26 23:10:02
【问题描述】:

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

当前在微调器 SelectedItem 绑定中不允许使用 Null 值

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

        <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 的布局如下:

<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如下:

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 类型的可观察集合。 ShipmentLots 的类是从 WCF 服务创建的。我将其扩展如下:

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 value not allowed 错误,并且 MvxListView 中的第一项在 MvxSpinner 上具有错误的颜色。随后的工作正常。我不确定第一个有什么不同。有人对此有任何想法吗?

谢谢, 吉姆

【问题讨论】:

  • @Stuart 你可以怎么看这个?它应该很简单,但是 MvxListView 第一行的微调器会显示微调器列表中的第一个值,即使绑定字段中存在另一个值。
  • 我相信这里已经回答了这个问题:github.com/MvvmCross/MvvmCross/issues/944

标签: c# android xamarin mvvmcross mvxbind


【解决方案1】:

在@Cheesebaron 和@Stuart 的大力帮助下,我发现如果您在MvxItemList 内的ItemTemplate 中使用MvxSpinner 或MvxAutoComplete,那么上面层次结构中的任何东西(包括MvxItemlist)都不能有android:layout_height="wrap_content"。原因是如果 Android 操作系统必须动态确定它们的高度,它就必须不止一次地绘制它们。所有的重绘都会在绑定中混淆。如果您将所有内容设置为固定高度,则一切正常。要解决上述问题,上述 MvxItemView 的标记应为

    <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

这最初是作为 repo 来演示错误的。一旦@Cheesebaron 向我展示了问题所在,我就对其进行了更正,因此这是一个可行的示例。希望这会对某人有所帮助。

【讨论】: