android - 如何在 MvxSpinner 中设置选中项

标签 android spinner xamarin mvvmcross

我有一个绑定(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/

相关文章:

javascript - 调用远程服务器期间 php 内的基本加载器微调器

java - Android 自定义微调器,带图像选择功能

ios - 如何在Xamarin iOS项目中无需从设备手动删除App的情况下更新启动画面?

Android SDK Manager 提示 "Loading SDK component information failed."

Android Spinner 值和文本

visual-studio - 在 Mac OS Visual Studio 上的 Xamarin 表单应用程序中,如何设置 iOS 构建级别?

android - 对象检测器上的 Tensorflow Lite GPU 支持

android - 发现多个文件具有独立于操作系统的路径 'lib/armeabi-v7a/libopencv_java3.so'

java - 来自子布局的 recyclerView 引用,包含在父布局中的 <include/>

android - 自定义形状的 TextView ,Android