我有一个带有 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 argument由 XAML 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/