我正在使用包含MvxSpinner的MvxListView。当我的应用程序运行时,跟踪会显示以下几个实例:
当前微调器SelectedItem绑定中不允许空值
我知道数据对象上的条目不为空。以下是相关代码: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的viewmodel包含一个名为Lots的Type ShipmentLot的observablecollection。 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值不允许错误,并且MvxListView中的第一项在MvxSpinner上的颜色错误。后续的工作正常。我不确定第一个有什么不同。有人对此有任何想法吗?
谢谢, 吉姆
答案 0 :(得分:1)
在@Cheesebaron和@Stuart的大力帮助下,我发现如果你在MvxItemList中的ItemTemplate中使用MvxSpinner或MvxAutoComplete,那么上面层次结构中包含MvxItemlist的任何东西都不能有android:layout_height =“wrap_content”。原因是如果Android OS必须动态确定它们的高度,它必须不止一次地绘制东西。所有重绘都会在绑定中产生混淆。如果你把所有东西都设置到一个固定的高度,一切都很好。要解决上述问题,上面的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
这是以回购来展示这个错误。一旦@Cheesebaron告诉我出了什么问题,我就更正了,所以这是一个有效的例子。希望这会对某人有所帮助。