c# - 在 ListViewRenderer 上使用 CachingStrategy 导致属性错误

标签 c# listview xamarin xamarin.forms

我有一个带有 ListView 的 View 。现在我切换到 custom renderer对于这个 ListView(顺便说一句,还有一个用于 ViewCell 的自定义渲染器)。如果我在模拟器(iOS、Android)中启动应用程序,我会收到以下异常:

Xamarin.Forms.Xaml.XamlParseException: Position 12:13. Cannot assign property "CachingStrategy": Property does not exists, or is not assignable, or mismatching type between value and property

如果我删除 CachingStrategy一切似乎都很好。这是我的代码:

ListView 所在的 View :

<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>

自定义 ListView

namespace MyApp.CustomRenderers
{
    public class CustomListView : ListView
    {
    }
}

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;
                }
            }
        }
    }
}

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

最佳答案

您收到此错误是因为 CachingStrategy 不是可绑定(bind)属性,而是 constructor argumentXAML parser or build task 提供.

选项-1

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

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

    }
}

并且,更改您的 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>

选项-2

有一个 hack 可用,您可以在其中创建自己的参数属性。但它仅在 XAML 编译打开时有效。更多详情 here .

关于c# - 在 ListViewRenderer 上使用 CachingStrategy 导致属性错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46610449/

相关文章:

C# - 试图找到剩余的 EventHandler 以防止控制超出范围

c# - 从 Windows 窗体应用程序退出代码

c# - 如何访问SD卡上的视频

c# - 具有 SqlDependency 的 IN 运算符不起作用

qt - 获取当前显示的委托(delegate)的索引 - QML ListView

grails - Groovy:对 View 中的列进行排序:列表

javascript - 使用 javascript 获取 Sharepoint ListView 元素的数量

ios - 如何在 Macbook 中的 iPhone 6s 模拟器上找到 SQLite 数据库?

xamarin - Xamarin 标签中可单击的部分文本

c# - OnGestureListener 在 Xamarin.Android 上的 fragment 中的 ListView 上捕获