我有一个绑定到的 MvxSpinner,List<PhotoCategory>
因此:
<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
为 PhotoCategory,其 PhotoCategoryId 与 SQLite 数据库中的匹配。但是,当显示微调器时,会显示默认值(我添加到PhotoCategories
属性中,PhotoCategory = 0, CategoryName="[Choose a Category]")。我发现的唯一修复是添加到视图中的这个(工作正常)代码:
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 作为参数值。
我错过了什么??