[英]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);
}
}
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:
但是,它是首选,应在以下情况下使用:
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.