在ListViewRenderer上使用CachingStrategy会导致属性错误

tes*_*ing 2 c# listview xamarin xamarin.forms

我有一个ListView关于它的观点.现在,我切换到自定义渲染这个ListView(并为自定义呈现ViewCellBTW).如果我在模拟器(iOS,Android)中启动应用程序,我会收到以下异常:

Xamarin.Forms.Xaml.XamlParseException:位置12:13.无法分配属性"CachingStrategy":属性不存在,或者不可分配,或者值和属性之间的类型不匹配

如果我删除CachingStrategy一切似乎工作正常.这是我的代码:

查看,ListView放置在哪里:

<StackLayout xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MyApp.Views.SomeView"
             xmlns:customViews="clr-namespace:MyApp.Views;assembly=MyApp"
             xmlns:renderer="clr-namespace:MyApp.CustomRenderers;assembly=MyApp"
             VerticalOptions="FillAndExpand">

  <renderer:CustomListView x:Name="SomeList"
            SeparatorColor="{StaticResource PrimaryLight}"
            HasUnevenRows="True"
            CachingStrategy="RecycleElement"
            IsGroupingEnabled="True" 
            GroupDisplayBinding="{Binding Key}"
            IsPullToRefreshEnabled="True"
            RefreshCommand="{Binding LoadDocumentsCommand}"
            IsRefreshing="{Binding IsBusy, Mode=OneWay}">
    <ListView.GroupHeaderTemplate>
      <DataTemplate>
        <customViews:GroupingHeader/>
      </DataTemplate>
    </ListView.GroupHeaderTemplate>

    <ListView.ItemTemplate>
      <DataTemplate>
        <customViews:MyListItem Clicked="Item_Clicked"/>
      </DataTemplate>
    </ListView.ItemTemplate>
  </renderer:CustomListView>

</StackLayout>
Run Code Online (Sandbox Code Playgroud)

CustomListView

namespace MyApp.CustomRenderers
{
    public class CustomListView : ListView
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

CustomListViewRenderer

[assembly: ExportRenderer(typeof(MyApp.CustomRenderers.CustomListView), typeof(MyApp.iOS.CustomRenderers.CustomListViewRenderer))]
namespace MyApp.iOS.CustomRenderers
{
    public class CustomListViewRenderer : ListViewRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<ListView> e)
        {
            base.OnElementChanged(e);

            if (this.Control != null)
            {
                var listView = (UITableView)this.Control;
                if (listView != null)
                {
                    listView.SeparatorInset = UIEdgeInsets.Zero;
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我应该复制属性还是需要不同的构造函数?

Sha*_*raj 9

您收到此错误,因为CachingStrategy它不是可绑定属性,而是由XAML解析器或构建任务提供的构造函数参数.

选项1

要解决此问题,您可以更改构造函数以接受CachingStrategy:

public class CustomListView : ListView
{
    public CustomListView(ListViewCachingStrategy cachingStrategy) : 
                base(cachingStrategy)
    {

    }
}
Run Code Online (Sandbox Code Playgroud)

并且,更改您的XAML以将缓存策略指定为构造函数参数:

<renderer:CustomListView x:Name="SomeList"
            HasUnevenRows="True"
            IsGroupingEnabled="True" 
            GroupDisplayBinding="{Binding Key}"
            IsPullToRefreshEnabled="True"
            RefreshCommand="{Binding LoadDocumentsCommand}"
            IsRefreshing="{Binding IsBusy, Mode=OneWay}">
    <x:Arguments>
        <ListViewCachingStrategy>RecycleElement</ListViewCachingStrategy>
    </x:Arguments>
    <ListView.GroupHeaderTemplate>
      <DataTemplate>
Run Code Online (Sandbox Code Playgroud)

OPTION-2

有一个hack可用,您可以在其中创建自己的参数属性.但它仅在XAML编译为ON时有效.更多细节在这里.