选择器项目列表和索引更新

时间:2017-03-24 09:29:33

标签: c# xamarin picker

我正在使用Xamarin Forms处理我的第一个应用程序,现在我正在努力解决一段代码,但却没有让它工作。如果有人能帮助我,我将感激不尽。

我有两个选择器,picker1和picker2,它们的Items和SelectedIndex属性在逻辑上相互连接。 picker2 SelectedIndex中的更改会导致picker1视图的Items和SelectedIndex属性的更新。我创建了一个自定义选取器类,它包含items源的可绑定属性,以使视图选择器Items属性相应地更新为viewmodel。

class BindablePicker : Picker
{

    public static BindableProperty ItemsSourceProperty =
        BindableProperty.Create<BindablePicker, IEnumerable>(o => o.ItemsSource, default(IEnumerable), propertyChanged: OnItemsSourceChanged);
    public IEnumerable ItemsSource
    {
        get { return (IEnumerable)GetValue(ItemsSourceProperty); }
        set { SetValue(ItemsSourceProperty, value); }
    }

    private static void OnItemsSourceChanged(BindableObject bindable, IEnumerable oldvalue, IEnumerable newvalue)
    {
        var picker = bindable as BindablePicker;
        picker.Items.Clear();
        if (newvalue != null)
        {

            foreach (var item in newvalue)
            {
                picker.Items.Add(item.ToString());
            }
        }
    }

}

在内容页面中创建和绑定视图对象:

        BindablePicker Level1Picker = new BindablePicker { Title = "Select" };
        Level1Layout.Children.Add(Level1Picker);

        Level1Picker.SetBinding(BindablePicker.ItemsSourceProperty, "Level1Items");
        Level1Picker.SetBinding(BindablePicker.SelectedIndexProperty, "Level1Index");

BindablePicker Level2Picker = new BindablePicker {Title =&#34; Select&#34; };             Level2Layout.Children.Add(Level2Picker);

        Level2Picker.SetBinding(BindablePicker.ItemsSourceProperty, "Level2Items");
        Level2Picker.SetBinding(BindablePicker.SelectedIndexProperty, "Level2Index");

最后是视图模型:

class EducationLevelViewModel1 : BaseViewModel
{

    public EducationLevelViewModel1(Criterion educationLevel)
    {
        this.EducationLevel = educationLevel;
    }
    Criterion EducationLevel { get; set; }


    string level1Choice;
    string Level1Choice
    {
        get { return level1Choice ?? (EducationLevel._Filters[0]._RelevanceType.ToString()); }
        set { level1Choice = value; }
    }


    List<string> level1Items;
    public List<string> Level1Items
    {
        get { return level1Items ?? (level1Items = GetLevel1List()); }
        set
        {
            level1Items = value;
            OnPropertyChanged();
        }
    }

    int level1Index = 0;

    public int Level1Index
    {
        get { return level1Index; }
        set
        {
            level1Index = value;
            OnPropertyChanged();
        }
    }

    void UpdateLevel1List()
    {
        Level1Choice = Level1Items[Level1Index];
        Level1Items = GetLevel1List();
        Level1Index =Level1Items.FindIndex(x => x == Level1Choice);

    }

    List<string> GetLevel1List()
    {

         if (Level2Items[(int)Level2Index] == Criterion.Filter.RelevanceType.Inadequacy.ToString())
             {
             return InadequacyItems;
         }
         else {
             return AllPickerItems;
         }

    }


    string level2Choice;
    public string Level2Choice
    {
        get { return level2Choice ?? (EducationLevel._Filters[1]._RelevanceType.ToString()); }
        set { level2Choice = value; }
    }

    List<string> level2Items;
    public List<string> Level2Items
    {
        get { return level2Items ?? (level2Items = GetLevel2List()); }
        set
        {
            level2Items = value;
            OnPropertyChanged();
        }
    }




    int level2Index = 0;

    public int Level2Index
    {
        get { return level2Index; }
        set
        {
            level2Index = value;
            UpdateLevel1List();
            OnPropertyChanged();
        }
    }

    }

} 如您所见,我的viewmodel实现了BaseViewModel类,后者又实现了INotifyPropertyChanged。 vm中的逻辑是picker2中的某个selectedindex正在触发picker1项列表和selectedIndex的更新。我在UpdateLevel1List()中这样做。请注意保存字符串选择,以便我可以在新列表中设置相同的picker1选择,否则我将其设置为索引0。

void UpdateLevel1List()
    {
        Level1Choice = Level1Items[Level1Index];
        Level1Items = GetLevel1List();
         Level1Index = Level1Items.FindIndex(x => x == Level1Choice) == -1 ? 0 : Level1Items.FindIndex(x => x == Level1Choice);

    }

问题: 因此,视图中的picker1项目列表的实际更新相应地更新了vm逻辑。但是新索引是NOT,它被设置为默认值(-1)。当我将picker2更改回默认选择时,该应用程序会崩溃。

我希望我能够清楚地了解我在这里想要实现的目标。

0 个答案:

没有答案