你能把一个 CarouselView 放在另一个 CarouselView 中吗?

Can you place a CarouselView in another CarouselView?

我想在第一个 CarouselView 中放置 2 个 ContentView,但随后我想在第二个 ContentView 中放置另一个 CarouselView。

下面的代码给出了“值不能为空”。当我从第二个 CarouselView 中删除 ItemTemplate 时,它​​会构建,但随后我在第二个 ContentView 上得到它(见图)。

<CarouselView x:Name="outerCarousel">
        <CarouselView.ItemsSource>
            <x:Array Type="{x:Type ContentView}">
                <ContentView>
                    <ContentView.Content>
                        <Frame BackgroundColor="Red" />
                    </ContentView.Content>
                </ContentView>
                <ContentView>
                    <ContentView.Content>
                        <Frame>
                            <CarouselView x:Name="innerCarousel" Margin="50">
                                <CarouselView.ItemsSource>
                                    <x:Array Type="{x:Type ContentView}">
                                        <ContentView>
                                            <ContentView.Content>
                                                <Frame BackgroundColor="Green" />
                                            </ContentView.Content>
                                        </ContentView>
                                        <ContentView>
                                            <ContentView.Content>
                                                <Frame BackgroundColor="Blue" />
                                            </ContentView.Content>
                                        </ContentView>
                                    </x:Array>
                                </CarouselView.ItemsSource>
                                <CarouselView.ItemTemplate>
                                    <DataTemplate>
                                        <ContentView Content="{Binding .}" />
                                    </DataTemplate>
                                </CarouselView.ItemTemplate>
                            </CarouselView>
                        </Frame>
                    </ContentView.Content>
                </ContentView>
            </x:Array>
        </CarouselView.ItemsSource>
        <CarouselView.ItemTemplate>
            <DataTemplate>
                <ContentView Content="{Binding .}" />
            </DataTemplate>
        </CarouselView.ItemTemplate>
    </CarouselView>

您可以参考下面的代码:

外部转盘:第 7 页

Xaml:

<ContentPage.Content>
    <CarouselView x:Name="outerCarousel" ItemsSource="{Binding outers}">
        <CarouselView.ItemsLayout>
            <LinearItemsLayout Orientation="Horizontal" />
        </CarouselView.ItemsLayout>
        <CarouselView.ItemTemplate>
            <DataTemplate>
                <ContentView Content="{Binding content}"></ContentView>
            </DataTemplate>
        </CarouselView.ItemTemplate>
    </CarouselView>
</ContentPage.Content>

隐藏代码:

 public partial class Page7 : ContentPage
{
    public Page7()
    {
        InitializeComponent();
        this.BindingContext = new OuterViewModel();
    }
}
public class Outer
{
    public ContentView content { get; set; }
}
public class OuterViewModel
{
    public ObservableCollection<Outer> outers { get; set; }
    public OuterViewModel()
    {
        outers = new ObservableCollection<Outer>()
        {
            new Outer(){ content=new View1()},
            new Outer(){ content=new View2()}
        };
    }
}

innerCarousel: View1, View2

视图 1:

<ContentView.Content>
    <Frame BackgroundColor="Red" />
</ContentView.Content>

视图 2:

<ContentView.Content>
    <CarouselView x:Name="innerCarousel" Margin="50">
        <CarouselView.ItemsLayout>
            <LinearItemsLayout Orientation="Vertical" />
        </CarouselView.ItemsLayout>
        <CarouselView.ItemsSource>
            <x:Array Type="{x:Type ContentView}">
                <ContentView>
                    <ContentView.Content>
                        <Frame BackgroundColor="Green" />
                    </ContentView.Content>
                </ContentView>
                <ContentView>
                    <ContentView.Content>
                        <Frame BackgroundColor="Blue" />
                    </ContentView.Content>
                </ContentView>
            </x:Array>
        </CarouselView.ItemsSource>
        <CarouselView.ItemTemplate>
            <DataTemplate>
                <ContentView Content="{Binding .}" />
            </DataTemplate>
        </CarouselView.ItemTemplate>
    </CarouselView>
</ContentView.Content>