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

C# 如何解决Xamarin表单ListView ViewCell重叠问题?,c#,listview,xamarin.forms,data-binding,frontend,C#,Listview,Xamarin.forms,Data Binding,Frontend,我有一个自定义ViewCell的ListView。binding ItemsSource是Biller类的列表。只有当我在屏幕上显示超过10个项目并滚动屏幕时,ViewCell重叠才会发生 Biller类定义 public class Biller { public string BillerId { get; set; } public string BillerName { get; set; } public string BillerShortName { get

我有一个自定义ViewCell的ListView。binding ItemsSource是Biller类的列表。只有当我在屏幕上显示超过10个项目并滚动屏幕时,ViewCell重叠才会发生

Biller类定义

public class Biller
{
    public string BillerId { get; set; }
    public string BillerName { get; set; }
    public string BillerShortName { get; set; }
    public string BillerLogoUrl { get; set; }
}
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);
        }
    }
自定义ViewCell定义

public class Biller
{
    public string BillerId { get; set; }
    public string BillerName { get; set; }
    public string BillerShortName { get; set; }
    public string BillerLogoUrl { get; set; }
}
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);
        }
    }
列表视图单元格重叠图像

这个问题应该是单元格的缓存没有回收,您可以为LsitView设置CachingStrategy=RecycleElement来解决这个问题

缓存策略指定ListView将尝试通过循环列表单元格来最小化其内存占用和执行速度。这种模式并不总是提供性能改进,应该进行测试以确定任何改进。但是,它是首选,应在以下情况下使用:

每个单元都有少量到中等数量的绑定。 每个单元格的BindingContext定义所有单元格数据。 每个单元格基本相似,单元格模板不变。 在XAML中,设置CachingStrategy属性,如下面的XAML中所示:


这个问题应该是单元格的缓存没有回收,您可以为LsitView设置CachingStrategy=RecycleElement来解决这个问题

缓存策略指定ListView将尝试通过循环列表单元格来最小化其内存占用和执行速度。这种模式并不总是提供性能改进,应该进行测试以确定任何改进。但是,它是首选,应在以下情况下使用:

每个单元都有少量到中等数量的绑定。 每个单元格的BindingContext定义所有单元格数据。 每个单元格基本相似,单元格模板不变。 在XAML中,设置CachingStrategy属性,如下面的XAML中所示:


更改服务器的缓存策略ListView@Jason成功了。谢谢!更改服务器的缓存策略ListView@Jason成功了。谢谢!