简体   繁体   English

如何解决 Xamarin 表单 ListView ViewCell 重叠问题?

[英]How to solve Xamarin Form ListView ViewCell Overlapping Issue?

I have a ListView with Customized ViewCell.我有一个带有自定义 ViewCell 的 ListView。 The binding ItemsSource is a list of Biller Class.绑定的 ItemsSource 是 Biller Class 的列表。 The ViewCell overlapping only happened when I'm displaying more than 10 items on the screen and I'm scrolling my screen.只有当我在屏幕上显示超过 10 个项目并且我正在滚动屏幕时,才会发生 ViewCell 重叠。

Biller Class Definition比勒 Class 定义

public class Biller
{
    public string BillerId { get; set; }
    public string BillerName { get; set; }
    public string BillerShortName { get; set; }
    public string BillerLogoUrl { get; set; }
}

Custom ViewCell Definition自定义 ViewCell 定义

public class CustomBillCell : ViewCell
    {
        public CustomBillCell()
        {
            // instantiate each of our views
            var billerShortNameLabel = new Label();
            var billerIdLabel = new Label();
            var billerNameLabel = new Label();
            var verticalLayout = new StackLayout();
            var horizontalLayout = new StackLayout();
            var billFrame = new Frame();
            var viewBillButton = new Button
            {
                Text = "View Bill",
                HorizontalOptions = LayoutOptions.EndAndExpand
            };

            // set bindings
            billerShortNameLabel.SetBinding(Label.TextProperty, new Binding("BillerShortName"));
            billerIdLabel.SetBinding(Label.TextProperty, new Binding("BillerId"));
            billerNameLabel.SetBinding(Label.TextProperty, new Binding("BillerName"));
            viewBillButton.SetBinding(ClassIdProperty, new Binding("BillerName"));
            viewBillButton.Clicked += ViewBillButtonClicked;

            // Set properties for desired design
            billerShortNameLabel.FontSize = 20;
            billerShortNameLabel.FontFamily = "d-din-bold";
            verticalLayout.Children.Add(billerShortNameLabel);
            verticalLayout.Children.Add(billerNameLabel);
            verticalLayout.Children.Add(billerIdLabel);
            verticalLayout.Spacing = 2;
            horizontalLayout.Orientation = StackOrientation.Horizontal;
            horizontalLayout.Children.Add(verticalLayout);
            horizontalLayout.Children.Add(viewBillButton);

            // Set properties for Frame
            billFrame.HasShadow = false;
            billFrame.CornerRadius = 10;
            billFrame.BorderColor = Color.FromHex("#0C0555");
            billFrame.Content = horizontalLayout;
            billFrame.Margin = 14;

            View = billFrame;
        }

        [Obsolete]
        private async void ViewBillButtonClicked(object sender, EventArgs e)
        {
            var billSignUpPage = new BillSignUpPage();
            Button btn = sender as Button;
            billSignUpPage.BindingContext = btn;
            await Application.Current.MainPage.Navigation.PushModalAsync(billSignUpPage);
        }
    }

ListView Cell Overlapping Image ListView 单元格重叠图像在此处输入图像描述

This problem should be the cache of Cell not recycling, you can set CachingStrategy="RecycleElement" for LsitView to solve that.这个问题应该是Cell的缓存没有回收,可以设置LsitView的CachingStrategy="RecycleElement" LsitView解决。

The RecycleElement caching strategy specifies that the ListView will attempt to minimize its memory footprint and execution speed by recycling list cells. RecycleElement缓存策略指定 ListView 将尝试通过回收列表单元来最小化其 memory 占用空间和执行速度。 This mode doesn't always offer a performance improvement, and testing should be performed to determine any improvements.此模式并不总是提供性能改进,应执行测试以确定任何改进。 However, it's the preferred choice, and should be used in the following circumstances:但是,它是首选,应在以下情况下使用:

  • Each cell has a small to moderate number of bindings.每个单元格都有少量到中等数量的绑定。
  • Each cell's BindingContext defines all of the cell data.每个单元格的 BindingContext 定义了所有单元格数据。
  • Each cell is largely similar, with the cell template unchanging.每个单元格都非常相似,单元格模板不变。

In XAML , set the CachingStrategy attribute as shown in the XAML below:XAML中,设置CachingStrategy属性,如下面的XAML所示:

<ListView CachingStrategy="RecycleElement">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
              ...
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在ListView Xamarin Forms中创建舍入的第一个和最后一个视单元? - How to create a rounded first and last viewcell in ListView Xamarin Forms? 如何通过索引从 xamarin 形式的 ListView 对象获取访问视单元? - How to get access viewcell by index from ListView object in xamarin forms? 如何从 Xamarin Forms 中的自定义 ViewCell 获取 ListView 项目索引? - How to get ListView item index from custom ViewCell in Xamarin Forms? Xamarin Forms - ListView 中的 iOS 动态 ViewCell 大小 - Xamarin Forms - iOS dynamic ViewCell size in a ListView 无法以viewcell xamarin形式加载图像 - Cannot load image in viewcell xamarin form 创建列表后,如何更新Xamarin Forms ListView的ViewCell属性? - How can I update a Xamarin Forms ListView's ViewCell properties after the list has been created? Xamarin.Forms中如何获取ListView的ViewCell数据并发送到另一个页面 - How to get ListView's ViewCell data and send it to another page in Xamarin.Forms Xamarin / C#:如何在不使用内部ListView的情况下遍历传递给viewCell的数组/列表DataMember - Xamarin / C# : How to iterate through Array/List DataMember passed to the viewCell without using inner ListView 如何在Xamarin Forms中的视单元中绑定堆栈布局? - How to bind a stacklayout in a viewcell in Xamarin Forms? 如何将参数传递给 Xamarin 中的自定义 ViewCell? - How to pass parameter to custom ViewCell in Xamarin?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM