我有一个绑定到List<PhotoCategory>
的MvxSpinner:
<Mvx.MvxSpinner
style="@style/Spinners"
android:id="@+id/photoCategorySpinner"
android:prompt="@string/photoCategory_prompt"
local:MvxBind="ItemsSource PhotoCategories; SelectedItem SelectedPhotoCategory; Visibility ShowPhotoFields, Converter=Visibility"
local:MvxDropDownItemTemplate="@layout/spinner_photocategories"
local:MvxItemTemplate="@layout/item_photocategory" />
SelectedItem绑定的SelectedPhotoCategory
也是PhotoCategory
。当此屏幕处于“更新模式”时,ViewModel将SelectedPhotoCategory
设置为PhotoCategoryId与SQLite数据库中的PhotoCategoryId匹配的PhotoCategory。但是,当显示微调器时,会显示默认值(我添加到PhotoCategories
属性,PhotoCategory = 0,CategoryName =“[选择类别]”)。我发现的唯一修复是这个(运行正常)代码添加到View:
protected override void OnCreate(Bundle bundle) {
base.OnCreate(bundle);
SetContentView(Resource.Layout.PhotoView);
//If we're in Update mode, select the relevant photo category in the spinner:
PhotoViewModel photoViewModel = (PhotoViewModel)ViewModel;
if (photoViewModel.ScreenMode == Constants.ScreenMode.Update) {
MvxSpinner photoCategorySpinner = FindViewById<MvxSpinner>(Resource.Id.photoCategorySpinner);
int itemPosition = 0;
int selectedPhotoCategoryId = photoViewModel.SelectedPhotoCategory.PhotoCategoryId;
foreach (PhotoCategory photoCategory in photoViewModel.PhotoCategories) {
if (photoCategory.PhotoCategoryId == selectedPhotoCategoryId) {
photoCategorySpinner.SetSelection(itemPosition);
}
itemPosition++;
}
}
我也尝试使用MvxSpinner.Adapter的GetPosition方法,但是对于PhotoCategoryId,CategoryName或SelectedPhotoCategory,它总是返回-1作为参数值。
我错过了什么?
答案 0 :(得分:4)
绑定
SelectedItem SelectedPhotoCategory
应该为您设置 - 并且应该使用Equals
找到要在微调器中选择的正确项目。
在https://github.com/slodge/MvvmCross-Tutorials/tree/master/ApiExamples
中使用SpinnerViewModel进行测试时,这似乎适用于最新的代码我知道最近有一个错误报告在其中一个绑定中使用==
与Equals
- 但我不认为这会影响微调器(参见https://github.com/slodge/MvvmCross/issues/309)