CarouselView的数据模板在Xamarin.Forms

时间:2018-03-26 14:41:51

标签: xamarin xamarin.forms xamarin.ios

我想为CarouselView创建DataTemplate,我可以在加载图像之前设置ActivityIndi​​cator。我已尝试通过以下方式但它会抛出错误。有人可以建议我吗?

我收到此错误: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>

1 个答案:

答案 0 :(得分:1)

  

我收到此错误:System.InvalidOperationException:DataTemplate返回非视图内容:&#39; TestPro.CacheImageCell&#39;。

查看定义here

的错误
object content = ((DataTemplate)type).CreateContent();
var view = content as View;
if(view == null)
    throw new InvalidOperationException($"DataTemplate returned non-view content: '{content}'.");

我们可以看到内容必须为View,但您在此使用的CacheImageCell不是ViewViewCell不是View的{​​{1}} }},您必须将其更改为ContentViewView