我想为DataTemplate创建CarouselView,在加载图像之前,我可以在这里设置ActivityIndicator。我试过这样做,但这会导致一个错误。有人能给我推荐一下吗?
我得到了这个错误: System.InvalidOperationException: DataTemplate返回了非视图内容:'TestPro.CacheImageCell‘。
CacheImageCell.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"
xmlns:local="clr-namespace:TestPro;assembly=TestPro"
xmlns:forms="clr-namespace:Xamarin.Forms;assembly=Xamarin.Forms.CarouselView"
xmlns:ff="clr-namespace:FFImageLoading.Forms;assembly=FFImageLoading.Forms"
x:Class="TestPro.CacheImageCell">
<ViewCell.View>
<StackLayout>
<ff:CachedImage x:Name="ProfileImage" Source="{Binding .}" Aspect="AspectFill" RelativeLayout.WidthConstraint=
"{ConstraintExpression Type=RelativeToParent, Property=Width}" HeightRequest="375">
</ff:CachedImage>
<ActivityIndicator BindingContext="{x:Reference ProfileImage}"
IsVisible="{Binding IsLoading}" IsRunning="{Binding IsLoading}" />
</StackLayout>
</ViewCell.View>
</ViewCell>CacheImageCell.cs
public partial class CacheImageCell : ViewCell
{
public CacheImageCell()
{
InitializeComponent();
}
protected override void OnBindingContextChanged()
{
base.OnBindingContextChanged();
var profile = (BindingContext as string);
}
}CacheImageSelector.cs
public class CacheImageSelector : DataTemplateSelector
{
private readonly DataTemplate cachingImageDataTemplate;
public CacheImageSelector()
{
cachingImageDataTemplate = new DataTemplate(typeof(CacheImageCell));
}
protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
{
var imageURL = item as string;
if (imageURL == null)
return null;
return cachingImageDataTemplate;
}
}UserProfile.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:local="clr-namespace:TestPro;assembly=TestPro"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:forms="clr-namespace:Xamarin.Forms;assembly=Xamarin.Forms.CarouselView"
xmlns:ff="clr-namespace:FFImageLoading.Forms;assembly=FFImageLoading.Forms"
x:Class="TestPro.UserProfile">
<ContentPage.Resources>
<ResourceDictionary>
<local:CacheImageCell x:Key="CacheImageCell"/>
<local:CacheImageSelector x:Key="CacheImageSelector" />
</ResourceDictionary>
</ContentPage.Resources>
<ContentPage.Content>
<StackLayout>
<forms:CarouselView ItemTemplate="{StaticResource CacheImageSelector}" x:Name="MainCarosel"
ItemsSource="{Binding Pictures}" Position="{Binding Position}"
IsVisible="{Binding IsImageVisible}"
HeightRequest="375">
</forms:CarouselView>
</StackLayout>
</ContentPage.Content>
</ContentPage>发布于 2018-03-27 07:55:03
我得到了这个错误: System.InvalidOperationException: DataTemplate返回了非视图内容:'TestPro.CacheImageCell‘。
查看此错误定义的这里
object content = ((DataTemplate)type).CreateContent();
var view = content as View;
if(view == null)
throw new InvalidOperationException($"DataTemplate returned non-view content: '{content}'.");我们可以看到内容必须是View,但是您在这里使用的CacheImageCell不是一种View,而是ViewCell,它不是从View派生出来的,您必须将其更改为ContentView或View。
https://stackoverflow.com/questions/49494179
复制相似问题