MvxSpinner selecteditem在项目源更改中未更新 [英] MvxSpinner selecteditem not updated on itemssource change

查看:50
本文介绍了MvxSpinner selecteditem在项目源更改中未更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am building a form that involves adding items from a list, but the list that is being shown to the user depends on the selection from another list. To do this I have two MvxSpinners each with a bound ItemsSource and 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_twoselected_item_two保持与更改前相同.我希望selected_item_two匹配item_source_two更改时在视觉上显示的内容.

When selected_item_one changes item_source_two changes to a new list. All of this works, except when item_source_two is changed selected_item_two remains the same as it was before the change. I would like for the selected_item_two to match what is visually shown when item_source_two changes.

推荐答案

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

The spinner binding isn't perfect in MvvmCross - in particular, the SelectedItem change must be sent after the ItemsSource change.

要解决此问题,您可以在ItemsSource之后 处发出伪造的SelectedItem更改信号-例如像这样:

To work around this, you could signal a fake SelectedItem change after the ItemsSource - e.g. something like:

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

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

If you wanted to fix this more fully, you could also consider overriding MvxSpinner - sadly you can't easily inherit because we missed virtual, but you do something like:

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

这篇关于MvxSpinner selecteditem在项目源更改中未更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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