如何MvxSpinner集中选择项目 [英] How to set selected item in MvxSpinner

查看:227
本文介绍了如何MvxSpinner集中选择项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个绑定到一个MvxSpinner 名单,其中,PhotoCategory> 是这样的:

I have an MvxSpinner that is bound to a List<PhotoCategory> thus:

<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势必也是一个 PhotoCategory 。当此屏幕中的更新模式,视图模型设置 SelectedPhotoCategory 来的PhotoCategoryId一个在SQLite数据库匹配PhotoCategory。然而,在显示微调时,默认值(这是我添加到 PhotoCategories 属性,PhotoCategory = 0,类别名称=[选择类别])表示。我发现的唯一的解决方法是这样的(它工作正常)code添加到视图:

The SelectedPhotoCategory that the SelectedItem is bound to is also a PhotoCategory. When this screen is in "update mode", the ViewModel sets the SelectedPhotoCategory to the PhotoCategory whose PhotoCategoryId matches the one in the SQLite database. However, when the spinner is displayed, the default value (which I add to the PhotoCategories property, PhotoCategory = 0, CategoryName="[Choose a Category]") is shown. The only fix I've found is this (which works ok) code added to the 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方法,但这个总是返回-1 PhotoCategoryId,类别名称或SelectedPhotoCategory作为参数值。

I've also tried using the GetPosition method of the MvxSpinner.Adapter but this always returns -1 for PhotoCategoryId, CategoryName or SelectedPhotoCategory as the parameter value.

我在想什么?

推荐答案

结合

  SelectedItem SelectedPhotoCategory

应该设置为你 - 并且应该使用等于找到正确的项目,在微调选择

should set this for you - and should use Equals to find the correct item to select in the spinner.

这当然似乎在最新code。使用SpinnerViewModel中的 https://github.com/slodge/MvvmCross-Tutorials/tree/master/ApiExamples

This certainly seems to work in the very latest code when testing using the SpinnerViewModel in https://github.com/slodge/MvvmCross-Tutorials/tree/master/ApiExamples

我知道有最近在使用报告中的错误 == 等于中的一种绑定 - 但我不认为这影响了微调(见 https://github.com/slodge/ MvvmCross /问题/ 309 )。

I know there was a bug reported recently on the use of == versus Equals in one of the bindings - but I don't think this effects the spinner (see https://github.com/slodge/MvvmCross/issues/309).

这篇关于如何MvxSpinner集中选择项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆