我有一个ListView
的视图。现在我为此ListView
(以及ViewCell
btw的自定义渲染器)切换到custom renderer。如果我在模拟器(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;
}
}
}
}
}
我应该复制属性还是需要不同的构造函数?
答案 0 :(得分:7)
您收到此错误是因为CachingStrategy
不是可绑定属性,而是constructor argument提供的XAML parser or build task。
要解决此问题,您可以更改构造函数以接受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>
您可以在其中创建自己的参数属性。但它仅在XAML编译为ON时有效。更多详情here。