ListView绑定如何在引擎盖下工作?

时间:2018-03-23 10:37:27

标签: c# xamarin.forms

我们最近遇到了一些并发问题,其中ObservableCollection绑定到iOS上的ListView ItemsSource 。调查这个问题是深入了解Xamarin Forms资源的一个很好的借口,因为我仍然在这个平台上沾沾自喜。

在Xamarin Forms中,ListView派生自ItemsView<Cell>。此类实现 ItemsSource 属性,如下所示:

public static readonly BindableProperty ItemsSourceProperty = BindableProperty.Create("ItemsSource", typeof(IEnumerable), typeof(ItemsView<TVisual>), null, propertyChanged: OnItemsSourceChanged);

public IEnumerable ItemsSource
{
    get { return (IEnumerable)GetValue(ItemsSourceProperty); }
    set { SetValue(ItemsSourceProperty, value); }
}

static void OnItemsSourceChanged(BindableObject bindable, object oldValue, object newValue)
{
    var element = newValue as Element;
    if (element == null)
        return;
    element.Parent = (Element)bindable;
}

现在我很困惑。通常,您可以为 ItemsSource 属性分配ObservableCollection或展平IEnumerable。这些不是来自Element ...所以如何 - 绑定到ObservableCollection工作?

0 个答案:

没有答案