Xamarin表单 - ListView性能 - 绑定(.xaml)与OnBindingContextChanged(.cs)

时间:2018-04-11 09:49:33

标签: c# xamarin binding xamarin.forms custom-cell

我正在开发一个Xamarin Forms应用程序,它需要一个ListView和CustomCells' MyCustomCell'。单元格具有中等数量的绑定(9),每个单元格大致相似,因此我决定将缓存策略设置为RecycleElement。此列表可以轻松包含100个项目。每个单元格包含两个带有Uri源的图像。

我通常在MyCustomCell.xaml中绑定视图的属性,例如使用Image:

<Image
    x:Name="MyImage"
    Source="{Binding ImageSource}"
    .../>

但我最近阅读了这篇文章https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/listview/performance,我想知道我的MyCustomCell.xaml和overrode方法之间的主要区别是什么?OnBindingContextChanged&#39;像这样:

protected override void OnBindingContextChanged()
{
    base.OnBindingContextChanged();
    var item = BindingContext as MyModel;
    MyImage.Source = ImageSource.FromUri(item.ImageSource);
}

我测试了两种解决方案,第二种似乎对我来说更快。我对吗 ?这两种解决方案之间的真正区别是什么?

1 个答案:

答案 0 :(得分:0)

我在项目中使用了ListView的优化,我也阅读了这个主题。我无法回答你关于OnBindingContext的问题,但为了加快你的listview,你应该检查CachingStrategy。这是一个真正的力量:) 但第一个例子比第二个好,因为OnBindingContext中的逻辑不是最好的做法 您应该遵循MVVM以获得干净且易于理解的代码和解决方案。