4

当我快速滚动列表时,每个单元格在数据加载之前显示为黑色。如果我慢慢滚动数据黑色单元格将不会出现。此行为仅发生在 Xamarin UWP 项目中。请找到下面的图片以供参考。在此处输入图像描述

4

2 回答 2

3

通过编写自定义 ViewCell 并使用本机数据模板修复了该问题。现在快速滚动没有黑色单元格。我发布我的答案,以便有人可能会发现它有用。

例如:如果您有要显示的名称列表,请执行以下操作:

首先添加自定义viewcell如下

        public class CustomViewCell : ViewCell
       {
        public static readonly BindableProperty NameProperty =
            BindableProperty.Create("Name", typeof(string), typeof(CustomViewCell), "");

        public string Name
        {
            get { return (string)GetValue(NameProperty); }
            set { SetValue(NameProperty, value); }
        }

       }

现在在 XAML 中添加 ListView,如下所示:

            <ListView 
            ItemsSource="{Binding Products}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <custom:CustomViewCell  Name="{Binding Name}"/>
                </DataTemplate>
            </ListView.ItemTemplate>
            </ListView>

然后你必须在 UWP 项目的 App.xaml 中编写 DateTemplate 样式,如下所示:

        <ResourceDictionary>
        <DataTemplate x:Key="CustomTemplate">
            <Grid Padding="10">
                <TextBlock  Foreground="#333333" FontSize="14" VerticalAlignment="Center" Text="{Binding Name"/>
            </Grid>
        </DataTemplate>
        </ResourceDictionary>

最后编写一个 CustomRenderer 将原生 viewcell 替换为我们的 ListView。

    public class CustomViewCellRenderer : ViewCellRenderer
    {
    public override Windows.UI.Xaml.DataTemplate GetTemplate(Cell cell)
    {
        return App.Current.Resources["CustomTemplate"] as Windows.UI.Xaml.DataTemplate;
    }
    }

现在列表完美运行,没有任何黑色单元格渲染问题。

于 2017-06-13T05:29:31.887 回答
2

这不是您的代码的问题。当您滚动得太快时,系统没有时间像滚动一样快地渲染单元格,因此会出现黑色单元格。

于 2017-06-05T21:56:15.887 回答