我有一个绑定(bind)到 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" />
SelectedPhotoCategory
SelectedItem 绑定(bind)到的也是一个 PhotoCategory
.当此屏幕处于“更新模式”时,ViewModel 设置 SelectedPhotoCategory
到其 PhotoCategoryId 与 SQLite 数据库中的相匹配的 PhotoCategory。但是,当显示微调器时,会显示默认值(我将其添加到 PhotoCategories
属性,PhotoCategory = 0, CategoryName="[Choose a Category]")。我发现的唯一修复是添加到 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 作为参数值。
我错过了什么??
最佳答案
绑定(bind)
SelectedItem SelectedPhotoCategory
应该为您设置它 - 并且应该使用 Equals
来找到要在微调器中选择的正确项目。
在 https://github.com/slodge/MvvmCross-Tutorials/tree/master/ApiExamples 中使用 SpinnerViewModel 进行测试时,这似乎在最新代码中有效。
我知道最近报告了一个关于在其中一个绑定(bind)中使用 ==
与 Equals
的错误 - 但我认为这不会影响微调器(参见 https://github.com/slodge/MvvmCross/issues/309 )。
关于android - 如何在 MvxSpinner 中设置选中项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17486448/