【问题标题】:MvxSpinner selecteditem not updated on itemssource changeMvxSpinner selecteditem 未在 itemssource 更改时更新
【发布时间】:2014-04-07 16:33:48
【问题描述】:

我正在构建一个涉及从列表中添加项目的表单,但是向用户显示的列表取决于从另一个列表中进行的选择。为此,我有两个 MvxSpinner,每个都有一个绑定的 ItemsSource 和 SelectedItem。

<MvxSpinner
   android:layout_width="300dp"
   android:layout_height="50dp"
   local:MvxBind="ItemsSource item_source_one; SelectedItem selected_item_one;"/>
<MvxSpinner
   android:layout_width="300dp"
   android:layout_height="50dp"
   local:MvxBind="ItemsSource item_source_two; SelectedItem selected_item_two;"/>

selected_item_one 更改时item_source_two 更改为新列表。所有这些都有效,除非 item_source_two 更改 selected_item_two 保持与更改前相同。我希望selected_item_twoitem_source_two 更改时视觉显示的内容相匹配。

【问题讨论】:

    标签: android xamarin mvvmcross


    【解决方案1】:

    MvvmCross 中的微调器绑定并不完美 - 特别是,SelectedItem 更改必须在 ItemsSource 更改之后发送。

    要解决此问题,您可以在ItemsSource之后更改SelectedItem - 例如,发送虚假SelectedItem类似:

     RaisePropertyChanged(() => item_source_two);
     RaisePropertyChanged(() => selected_item_two);
    

    如果您想更全面地解决此问题,您还可以考虑覆盖 MvxSpinner - 遗憾的是,您无法轻松继承,因为我们错过了虚拟,但您可以执行以下操作:

    public class MySpinner : MvxSpinner
    {
        public MySpinner(Context context, IAttributeSet attrs)
            : base(context, attrs)
        {
        }
    
        [MvxSetToNullAfterBinding]
        public new IEnumerable ItemsSource
        {
            get { return base.ItemsSource; }
            set 
            { 
                // TODO - work out what the current selection and store it in a local variable
                base.ItemsSource = value; 
                // TODO - reset the current selection here from the local variable
            }
        }
    }
    

    继承自https://github.com/MvvmCross/MvvmCross/blob/v3.1/Cirrious/Cirrious.MvvmCross.Binding.Droid/Views/MvxSpinner.cs

    【讨论】:

      【解决方案2】:

      我不得不这样做......

      public class MySpinner : MvxSpinner
      {
         public MySpinner(Context context, IAttributeSet attrs):base(context,attrs)
      {
      }
      
      [MvxSetToNullAfterBinding]
      public new IEnumerable ItemsSource
      {
          get { return base.ItemsSource; }
          set 
          { 
             base.ItemsSource = null;            
             base.ItemsSource = value;     
          }
      }
      }
      

      在您键入之后,该集合已更新! :)

      【讨论】:

        猜你喜欢
        • 2021-09-26
        • 2019-03-18
        • 2013-04-25
        • 2011-07-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-17
        相关资源
        最近更新 更多