C# 以Xamarin形式为每张幻灯片提供不同模板的CarouseView

C# 以Xamarin形式为每张幻灯片提供不同模板的CarouseView,c#,xaml,templates,xamarin.forms,carousel,C#,Xaml,Templates,Xamarin.forms,Carousel,我需要在Xamarin表单中创建一个旋转视图,其中每张幻灯片都有一个特定的模板。 目前我已经这样做了: XAML: xmlns:control="clr-namespace:Xamarin.Forms;assembly=Xamarin.Forms.CarouselView" ....... <ContentView HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"> <co

我需要在Xamarin表单中创建一个旋转视图,其中每张幻灯片都有一个特定的模板。 目前我已经这样做了:

XAML

xmlns:control="clr-namespace:Xamarin.Forms;assembly=Xamarin.Forms.CarouselView"
.......

 <ContentView HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
          <control:CarouselView x:Name="carouselView">
            <control:CarouselView.ItemTemplate>
              <DataTemplate>
                <Label Text="{Binding Testo}" />
              </DataTemplate>
            </control:CarouselView.ItemTemplate>
          </control:CarouselView>
        </ContentView>
public class CustomCell
{
    public string Testo { get; set; }
}
所有这些都是有效的,我的问题是每张幻灯片都有不同的模板,例如,每张幻灯片都有不同的图形网格,这是因为我必须以不同的图形方式显示数据。 你能推荐一个解决方案吗?谢谢

您可以使用自定义旋转视图中不同项目的外观。一个简单的例子:

MyDataTemplateSelector.cs

public class MyDataTemplateSelector : DataTemplateSelector
{
    public DataTemplate SimpleTemplate { get; set; }
    public DataTemplate ComplexTemplate { get; set; }

    public MyDataTemplateSelector()
    {
        SimpleTemplate = new DataTemplate(typeof(SimpleView));
        ComplexTemplate = new DataTemplate(typeof(ComplexView));
    }

    protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
    {
        CustomCell cell = (CustomCell)item;

        if (cell.Testo.Length > 5) {
            return ComplexTemplate;
        } else {
            return SimpleTemplate;
        }
    }
}
SimpleView.xaml

<ContentView>
    <StackLayout BackgroundColor="Red">
      <Label Text="{Binding Testo}" VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand" />
    </StackLayout>
</ContentView>

ComplexView.xaml

<ContentView>
    <StackLayout BackgroundColor="Yellow" >
      <Label Text="{Binding Testo}" VerticalOptions="CenterAndExpand" HorizontalOptions="Center" />
      <Label Text="I lied about this being complex" />
    </StackLayout>
</ContentView>

在您的旋转视图所在的页面中:

<ContentPage.Resources>
  <ResourceDictionary>
    <local:MyDataTemplateSelector x:Key="templateSelector"></local:MyDataTemplateSelector>
  </ResourceDictionary>
</ContentPage.Resources>

....

<control:CarouselView x:Name="carouselView" ItemTemplate="{StaticResource templateSelector}" />

....

问题:CarouseView未公开名为“ItemTemplate”的字段。您能否调整示例以演示如何正确分配该属性?CarouseView确实公开了该属性。。。如果文档中已经有
节点,它就不会在Intellisense中显示。
<ContentPage.Resources>
  <ResourceDictionary>
    <local:MyDataTemplateSelector x:Key="templateSelector"></local:MyDataTemplateSelector>
  </ResourceDictionary>
</ContentPage.Resources>

....

<control:CarouselView x:Name="carouselView" ItemTemplate="{StaticResource templateSelector}" />