Xamarin form-UWP - 在快速滚动列表时数据可见之前显示黑色单元格

Ren*_*ith 5 xamarin.forms uwp xamarin.uwp

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

Ren*_*ith 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); }
        }

       }
Run Code Online (Sandbox Code Playgroud)

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

            <ListView 
            ItemsSource="{Binding Products}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <custom:CustomViewCell  Name="{Binding Name}"/>
                </DataTemplate>
            </ListView.ItemTemplate>
            </ListView>
Run Code Online (Sandbox Code Playgroud)

然后你必须在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>
Run Code Online (Sandbox Code Playgroud)

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

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

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