XAML 具有多个项目但未数据绑定的Carousal视图

oknwwptz  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(100)

如何将静态文本设置为Caroal视图项,而不是进行数据绑定?在下面的代码中,我尝试在用户向左滑动时显示第二页。这段代码说属性ItemTemplate设置了多次

<StackLayout Margin="10">
        <CarouselView>
            <CarouselView.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Label Text="Page 1"/>
                    </Grid>
                </DataTemplate>
            </CarouselView.ItemTemplate>
            <CarouselView.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Label Text="Page 2"/>
                    </Grid>
                </DataTemplate>
            </CarouselView.ItemTemplate>
        </CarouselView>
    </StackLayout>
jucafojl

jucafojl1#

如果您想在xaml本身中静态地设置这些项而不进行数据绑定,则可以使用string类型的Array markup extension

<StackLayout Margin="10">
       <CarouselView>
            <CarouselView.ItemsSource>
                <x:Array Type="{x:Type sys:String}">
                    <sys:String>Page 1</sys:String>
                    <sys:String>Page 2</sys:String>
                </x:Array>
            </CarouselView.ItemsSource>

            <CarouselView.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Label Text="{Binding .}" />
                    </Grid>
                </DataTemplate>
            </CarouselView.ItemTemplate>
        </CarouselView>
    </StackLayout>

PS:不要忘记添加所需的xaml命名空间:

xmlns:sys="clr-namespace:System;assembly=mscorlib"

相关问题