【问题标题】:Using CachingStrategy on ListViewRenderer causes property error在 ListViewRenderer 上使用 CachingStrategy 会导致属性错误
【发布时间】:2017-10-06 16:48:34
【问题描述】:

我有一个带有ListView 的视图。现在我为这个ListView 切换到custom renderer(顺便说一下ViewCell 的自定义渲染器)。如果我在模拟器(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>

CustomListView

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

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

【问题讨论】:

    标签: c# listview xamarin xamarin.forms


    【解决方案1】:

    您收到此错误是因为CachingStrategy 不是可绑定属性,而是由XAML parser or build task 提供的constructor argument

    选项-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 编译为 ON 时有效。更多详情here.

    【讨论】:

    • 我选择了选项号。 1,因为我不想使用 hack。因此,我添加了一个无参数构造函数,它使用 RetainElement 作为默认值调用您提到的构造函数。现在,如果有人忘记使用x:Arguments,我不会得到例外。
    猜你喜欢
    • 2013-10-27
    • 1970-01-01
    • 2017-12-16
    • 2016-07-29
    • 1970-01-01
    • 2017-08-01
    • 2018-09-28
    • 2020-04-06
    • 2018-03-22
    相关资源
    最近更新 更多