首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CarouselView抛出DataTemplate的数据模板Xamarin.Forms中返回的非视图内容

CarouselView抛出DataTemplate的数据模板Xamarin.Forms中返回的非视图内容
EN

Stack Overflow用户
提问于 2018-03-26 14:41:51
回答 1查看 398关注 0票数 0

我想为DataTemplate创建CarouselView,在加载图像之前,我可以在这里设置ActivityIndicator。我试过这样做,但这会导致一个错误。有人能给我推荐一下吗?

我得到了这个错误: System.InvalidOperationException: DataTemplate返回了非视图内容:'TestPro.CacheImageCell‘。

CacheImageCell.xaml

代码语言:javascript
复制
<?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

代码语言:javascript
复制
public partial class CacheImageCell : ViewCell
{
    public CacheImageCell()
    {
        InitializeComponent();
    }
    protected override void OnBindingContextChanged()
    {
        base.OnBindingContextChanged();
        var profile = (BindingContext as string);
    }
}

CacheImageSelector.cs

代码语言:javascript
复制
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

代码语言:javascript
复制
<?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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-03-27 07:55:03

我得到了这个错误: System.InvalidOperationException: DataTemplate返回了非视图内容:'TestPro.CacheImageCell‘。

查看此错误定义的这里

代码语言:javascript
复制
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派生出来的,您必须将其更改为ContentViewView

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49494179

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档