【问题标题】:Data Template for CarouselView throws error of DataTemplate returned non-view content in Xamarin.FormsCarouselView 的数据模板在 Xamarin.Forms 中引发 DataTemplate 返回非视图内容的错误
【发布时间】:2018-03-27 15:55:03
【问题描述】:

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

我收到此错误:System.InvalidOperationException: DataTemplate returned non-view content: '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>

【问题讨论】:

    标签: xamarin xamarin.forms xamarin.ios


    【解决方案1】:

    我收到此错误:System.InvalidOperationException: DataTemplate returned non-view content: 'TestPro.CacheImageCell'。

    看看这个错误定义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不是View的一种,它是ViewCell,它不是从View派生的,你必须把它改成@ 987654328@或View

    【讨论】:

    • 现在看起来不错。但是,全面测试仍在进行中。我一定会对此进行更新。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 2020-06-05
    • 1970-01-01
    • 2018-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-27
    • 1970-01-01
    相关资源
    最近更新 更多