xamarin.forms carouselview不同的内容页

xamarin.forms carouselview不同的内容页,xamarin.forms,carousel,Xamarin.forms,Carousel,我想在我的内容页中添加一个旋转视图,在滑块视图中使用不同的模板。我是否可以在设计页面中创建3个数据模板,如下所示: <ctrl:CarouselViewControl x:Name="test" BackgroundColor="LightGreen" HeightRequest="250" WidthRequest="250" InterPageSpacing="10" ShowIndicators="True" IndicatorsShape="Circle" IndicatorsTi

我想在我的内容页中添加一个旋转视图,在滑块视图中使用不同的模板。我是否可以在设计页面中创建3个数据模板,如下所示:

<ctrl:CarouselViewControl x:Name="test" BackgroundColor="LightGreen" HeightRequest="250" WidthRequest="250" InterPageSpacing="10" ShowIndicators="True" IndicatorsShape="Circle" IndicatorsTintColor="White" Position="0">
            <ctrl:CarouselViewControl.ItemTemplate>
                <DataTemplate1>

                </DataTemplate1>

                <DataTemplate2>

                </DataTemplate2>

                <DataTemplate3>

                </DataTemplate3>
            </ctrl:CarouselViewControl.ItemTemplate>
        </ctrl:CarouselViewControl>

当我向左或向右滑动时,我想选择特定的Datatemplate,因为我想在一个contentpage中使用不同的标签、Imagebutton或其他内容


谢谢。

您可以为不同的DataTemplates创建DataTemplateSelector

这里是运行GIF

代码类似于folliwng格式

       <ContentPage.Resources>
    <ResourceDictionary>
        <DataTemplate x:Key="Page1Template" >
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="*" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <Label Text="Top Left" Grid.Row="0" Grid.Column="0" />
                <Label Text="Top Right" Grid.Row="0" Grid.Column="1" />
                <Label Text="Bottom Left" Grid.Row="1" Grid.Column="0" />
                <Label Text="Bottom Right" Grid.Row="1" Grid.Column="1" />
            </Grid>
            <!--<ViewCell>
                <StackLayout>
                    <Button Text="ccccc"/>
                    <Button Text="cccc"/>
                    <Button Text="ccccc"/>
                    <Label Text="ccccccc"/>
                    <Button Text="cccc"/>
                    <Button Text="ccccc"/>
                </StackLayout>

            </ViewCell>-->
        </DataTemplate>

        <DataTemplate x:Key="Page2Template">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="*" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <Button Text="button1" Grid.Row="0" Grid.Column="0" />
                <Label Text="Top Right" Grid.Row="0" Grid.Column="1" />
                <Button Text="button2" Grid.Row="1" Grid.Column="0" />
                <Label Text="Bottom Right" Grid.Row="1" Grid.Column="1" />
            </Grid>
        </DataTemplate>

        <DataTemplate x:Key="Page3Template">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="*" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <Button Text="button1" Grid.Row="0" Grid.Column="0" />
                <Button Text="Top Right" Grid.Row="0" Grid.Column="1" />
                <Button Text="button2" Grid.Row="1" Grid.Column="0" />
                <Button Text="Bottom Right" Grid.Row="1" Grid.Column="1" />
            </Grid>
        </DataTemplate>

        <local:PersonDataTemplateSelector x:Key="personDataTemplateSelector"

            Page1="{StaticResource  Page1Template}"
            Page2="{StaticResource  Page2Template}" 
            Page3="{StaticResource  Page3Template}"/>
    </ResourceDictionary>
</ContentPage.Resources>
这是数据模板选择器的代码

    public class PersonDataTemplateSelector : DataTemplateSelector
{
    public DataTemplate Page1 { get; set; }
    public DataTemplate Page2 { get; set; }

    public DataTemplate Page3 { get; set; }

    public DataTemplate Page4 { get; set; }

    protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
    {
        switch (((MyText)item).Page)
        {
            case 1:
                return Page1;
            case 2:
                return Page2;
            case 3:
                return Page3;
            default:
                return Page1;

        }

    }
}
如果您想了解更多详细信息,可以参考以下链接

这是我的
MyText

public class MyText
{
    public string LabelText { get; set; }
    public int Page { get; set; }
}
我的主页反码

            ObservableCollection<MyText> observableCollection = new ObservableCollection<MyText>();
        observableCollection.Add(new MyText { LabelText="xxxxx", Page=1 });
        observableCollection.Add(new MyText { LabelText = "bbbbb", Page = 2 });
        observableCollection.Add(new MyText { LabelText = "ccccc", Page = 3 });




        mycal.ItemsSource = observableCollection;
ObservableCollection ObservableCollection=新的ObservableCollection();
添加(新的MyText{LabelText=“xxxxx”,Page=1});
添加(新的MyText{LabelText=“bbbbb”,Page=2});
添加(新的MyText{LabelText=“ccccccc”,Page=3});
mycal.ItemsSource=可观察到的采集;
            ObservableCollection<MyText> observableCollection = new ObservableCollection<MyText>();
        observableCollection.Add(new MyText { LabelText="xxxxx", Page=1 });
        observableCollection.Add(new MyText { LabelText = "bbbbb", Page = 2 });
        observableCollection.Add(new MyText { LabelText = "ccccc", Page = 3 });




        mycal.ItemsSource = observableCollection;