1

我正在编写一个包含项目数据库的简单应用程序。这些项目具有类型、制造商、型号和一些其他属性。我已经实现了三个带有 MvxPickerViewModel 的 UIPickerView,如 MvvmCross 的 N+1 系列的 N=19 中所述。每种类型、制造商和模型都有一个 UIPickerView/MvxPickerViewModel(一次只有一个出现在屏幕上)。但是,如果我更新 MvxPickerViewModel 的 ItemSource 数据,UIPickerView 中已经可见的行在滚动出屏幕之前不会刷新。N=19 示例不会更新 UIPickerView 中的项目列表,因此不清楚该问题是否存在。我犯了错误还是有其他人经历过这种情况?有解决办法吗?

        public override void ViewDidLoad ()
    {
        base.ViewDidLoad ();

        NavigationController.NavigationBarHidden = true;

        var comparableTableViewSource = new MvxStandardTableViewSource(ComparableLV);
        ComparableLV.Source = comparableTableViewSource;

        var ManufacturerPicker = new UIPickerView();
        var manufacturerPickerModel = new MvxPickerViewModel(ManufacturerPicker);
        ManufacturerPicker.Model = manufacturerPickerModel;
        ManufacturerPicker.ShowSelectionIndicator = true;
        ManufacturerTextField.InputView = ManufacturerPicker;

        var ModelPicker = new UIPickerView();
        var modelPickerModel = new MvxPickerViewModel(ModelPicker);
        ModelPicker.Model = modelPickerModel;
        ModelPicker.ShowSelectionIndicator = true;
        ModelTextField.InputView = ModelPicker;

        var TypePicker = new UIPickerView();
        var typePickerModel = new MvxPickerViewModel(TypePicker);
        TypePicker.Model = typePickerModel;
        TypePicker.ShowSelectionIndicator = true;
        TypeTextField.InputView = TypePicker;

        var set = this.CreateBindingSet<FirstView, FirstViewModel>();
        set.Bind(comparableTableViewSource).For(s => s.ItemsSource).To(vm => vm.Comparables);
        set.Bind(manufacturerPickerModel).For(p => p.ItemsSource).To(vm => vm.Manufacturers);
        set.Bind(manufacturerPickerModel).For(p => p.SelectedItem).To(vm => vm.SelectedManufacturer);
        set.Bind(ManufacturerTextField).To(vm => vm.SelectedManufacturer);
        set.Bind(modelPickerModel).For(p => p.ItemsSource).To(vm => vm.Models);
        set.Bind(modelPickerModel).For(p => p.SelectedItem).To(vm => vm.SelectedModel);
        set.Bind(ModelTextField).To(vm => vm.SelectedModel);
        set.Bind(typePickerModel).For(p => p.ItemsSource).To(vm => vm.Types);
        set.Bind(typePickerModel).For(p => p.SelectedItem).To(vm => vm.SelectedType);
        set.Bind(TypeTextField).To(vm => vm.SelectedType);

        set.Apply();

        var g = new UITapGestureRecognizer(() => {
            HornTextField.ResignFirstResponder();
            ManufacturerTextField.ResignFirstResponder();
            ModelTextField.ResignFirstResponder();
        });

        View.AddGestureRecognizer(g);
    }
4

1 回答 1

2

Looking at MvxPickerViewModel.cs I'm suspicious that there is no call to ReloadAllComponents (or to ReloadComponent[0]) when the ItemsSource itself changes, but there is a call when the Collection internally changes.

As a workaround, perhaps try a subclass like:

public class MyPickerViewModel
    : MvxPickerViewModel
{
    private readonly UIPickerView _pickerView;

    public MyPickerViewModel(UIPickerView pickerView)
      : base(pickerViww)
    {
        _pickerView = pickerView;
    }


    [MvxSetToNullAfterBinding]
    public override  IEnumerable ItemsSource
    {
        get { return base.ItemsSource; }
        set
        {
            base.ItemsSource = value;
            if (value != null)
               _pcikerView.ReloadComponent(0);
        }
    }  
}

Would also be great to get a fix back into MvvmCross...

于 2013-07-15T07:10:52.843 回答