我的android视图中有三个MvxSpinners。 这些微调器绑定到三个不同的列表。 对于这些微调器,数据绑定模式是TwoWay。当这个观点是 显示时,所有这三个微调器都会显示预定义值。
When user change the value in first spinner,then second spinner will be clear and
get loaded with new values based on the selected value in first spinner.
我怎样才能做到这一点?
答案 0 :(得分:1)
有很多方法可以实现这一点,代码放置真的取决于你。总体而言,我们的想法是拥有一个“SelectedItem”对象,您可以将其传递给您的方法并“加载”下一个List。
请记住,此代码是更传统的MVVM,但可以轻松转换为MVVMCross等效。我相信所有这些类型都应该得到MVVMCross的支持。
private MyFirstObject _selectedFirstObject;
public MyFirstObject SelectedFirstObject
{
get { return _selectedFirstObject; }
set
{
_selectedFirstObject = value;
RaisePropertyChanged("SelectedFirstObject");
if(value != null)
LoadMySecondObjects(value);
}
}
private ObservableCollection<MyFirstObject> _myFirstObjects;
public ObservableCollection<MyFirstObject> MyFirstObjects
{
get { return _myFirstObjects; }
set
{
_myFirstObjects = value;
RaisePropertyChanged("MyFirstObjects");
}
}
private ObservableCollection<MySecondObject> _mySecondObjects;
public ObservableCollection<MySecondObject> MySecondObjects
{
get { return _mySecondObjects; }
set
{
_mySecondObjects = value;
RaisePropertyChanged("MySecondObjects");
}
}
public void LoadMySecondObjects(MyFirstObject current)
{
//Wherever you're pulling data from
MySecondObjects = MyDataService.GetAll(current);
}
protected void RaisePropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
public event PropertyChangedEventHandler PropertyChanged;
答案 1 :(得分:0)
我遇到了同样的问题但是只有在你的ItemsSource中添加空值(作为默认值)并尝试将SelectedItem重置为null时。 SelectedItem在ViewModel中更改,但不在微调器中更改。在那种情况下有很多解决方案,但我使用消息来通知View设置所选项目
public class SpinnerSelectionChanged : MvxMessage
{
public SpinnerSelectionChanged(object sender, string spinnerName, int position): base(sender)
{
SpinnerName = spinnerName;
Position = position;
}
public string SpinnerName { get; set; }
public int Position { get; set; }
}
在视图
中private void OnSpinnerSelectionChanged(SpinnerSelectionChanged obj)
{
switch (obj.SpinnerName)
{
case "City":
_spinnerCity.SetSelection(obj.Position);
break;
case "Office":
_spinnerOffice.SetSelection(obj.Position);
break;
}
}