我有一个带有列表视图的xaml,看起来像这样
<?xml version="1.0" encoding="utf-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:myControls="clr-namespace:MyControls;assembly=App.Visuals"
x:Class="SearchResultPage">
<ContentPage.Resources>
<ResourceDictionary>
<myControls:TravelCardItemTemplateSelector x:Key="TravelResultTemplate" />
<myControls:TravelSearchHeaderTemplateSelector x:Key="HeaderTemplateSelector" />
</ResourceDictionary>
</ContentPage.Resources>
<ContentPage.Content>
<ListView Grid.Row ="2"
CachingStrategy="RecycleElement"
IsRefreshing="{Binding IsBusy}"
IsPullToRefreshEnabled="true"
RefreshCommand="{Binding RefreshCommand}"
ItemsSource="{Binding SearchResults}"
Style="{DynamicResource ListStyle}"
HasUnevenRows="True"
ItemTemplate="{StaticResource TravelResultTemplate}"
HeaderTemplate="{StaticResource HeaderTemplateSelector}"/>
</ContentPage.Content>
</ContentPage>
模板选择器看起来像这样
public class TravelSearchHeaderTemplateSelector : DataTemplateSelector
{
protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
{
if (item== null)
{
return null;
}
var headerTempleTemplate= new DataTemplate(typeof(TransportSearchHeaderPanelCell));
return headerTempleTemplate;
}
}
模板的xaml是一个看起来像这样的视图单元
<?xml version="1.0" encoding="UTF-8"?>
<ViewCell xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="MyControls.TransportSearchHeaderPanelCell">
<ViewCell.View> <Grid ColumnSpacing="0" RowSpacing="0"> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> <ColumnDefinition Width="*" /><ColumnDefinition Width="*" /></Grid.ColumnDefinitions>
<Button Text="Price" Grid.Column="0" />
<Button Text="Departure" Grid.Column="1" BackgroundColor="Transparent"/>
<Button Text="Fastest" Grid.Column="2" BackgroundColor="Transparent"/>
</Grid>
</ViewCell.View>
</ViewCell>
在运行时,应用程序抛出异常
{System.InvalidOperationException: LoadTemplate should not be null
at Xamarin.Forms.ElementTemplate.CreateContent () [0x00008] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Core\ElementTemplate.cs:74
at Xamarin.Forms.ListView.ValidateHeaderFooterTemplate (Xamarin.Forms.BindableObject bindable, System.Object value) [0x0000b] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Core\ListView.cs:587
at Xamarin.Forms.BindableObject.SetValueCore (Xamarin.Forms.BindableProperty property, System.Object value, Xamarin.Forms.Internals.SetValueFlags attributes, Xamarin.Forms.BindableObject+SetValuePrivateFlags privateAttributes) [0x00071] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Core\BindableObject.cs:359
at Xamarin.Forms.BindableObject.SetValue (Xamarin.Forms.BindableProperty property, System.Object value, System.Boolean fromStyle, System.Boolean checkAccess) [0x0005f] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Core\BindableObject.cs:542
at Xamarin.Forms.BindableObject.SetValue (Xamarin.Forms.BindableProperty property, System.Object value) [0x00000] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Core\BindableObject.cs:83
at Xamarin.Forms.ListView.set_HeaderTemplate (Xamarin.Forms.DataTemplate value) [0x00000] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Core\ListView.cs:164 }
绑定应该足够简单,为什么会有例外。
答案 0 :(得分:1)
正在搜索我遇到的错误消息this bug report,它似乎已经是一个相当老的错误了。既然你已经验证它可以从代码隐藏中运行,那么它似乎仍然存在。
现在唯一的办法是从代码隐藏处理它并等待它们修复它。或者,如果你足够勇敢;因为Xamarin.Forms是开源的,所以自己修复它。