Android MvxSpinner selecteditem未在项目资源更改时更新
我正在构建一个表单,其中包括从列表中添加项目,但显示给用户的列表取决于从另一个列表中进行的选择。为此,我有两个MVXSpinner,每个都有一个绑定的ItemsSource和SelectedItemAndroid MvxSpinner selecteditem未在项目资源更改时更新,android,xamarin,mvvmcross,Android,Xamarin,Mvvmcross,我正在构建一个表单,其中包括从列表中添加项目,但显示给用户的列表取决于从另一个列表中进行的选择。为此,我有两个MVXSpinner,每个都有一个绑定的ItemsSource和SelectedItem <MvxSpinner android:layout_width="300dp" android:layout_height="50dp" local:MvxBind="ItemsSource item_source_one; SelectedItem selected_it
<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\u item\u one
更改item\u source\u two
更改为新列表时。所有这些都有效,除非item\u source\u two
更改selected\u item\u two
与更改前保持不变。我希望selected\u item\u two
与item\u source\u two
更改时显示的内容相匹配。微调器绑定在MvvmCross中并不完美-尤其是SelectedItem
更改必须在ItemsSource
更改之后发送
要解决此问题,您可以在项源
之后发出假SelectedItem
更改-例如:
RaisePropertyChanged(() => item_source_two);
RaisePropertyChanged(() => selected_item_two);
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
}
}
}
<>如果你想更全面地解决这个问题,你也可以考虑重写MVXSPIN——很遗憾,你不能轻易继承,因为我们错过了Virtual,但是你做了一些类似的事情:
RaisePropertyChanged(() => item_source_two);
RaisePropertyChanged(() => selected_item_two);
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
}
}
}
继承自我不得不这样做
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;
}
}
}
之后,该集合将在您键入时更新!:)