为了账号安全,请及时绑定邮箱和手机立即绑定

Xamarin.forms 轮播视图列表列表

Xamarin.forms 轮播视图列表列表

C#
慕姐4208626 2023-09-24 10:46:09
我有一个使用轮播视图的 xamarin.forms 应用程序。我的轮播视图每个视图有 6 个项目。但现在它每次视图显示一个项目。我知道问题是我需要创建一个List 的 List。但我被困在这个问题上。我预期的轮播视图输出是这样的。但我现在得到的是这样的我原来的包含数据的列表是这样的。 ObservableCollection<SECHomescreenData> resultObjForSEC = callForSECtilesScreen.APICallResult<ObservableCollection<SECHomescreenData>>();我的 SECHomescreenData。  public partial class SECHomescreenData   {               public string Status { get; set; }           public string Countered { get; set; }   }当我将此列表绑定到我的轮播视图时,它只会在每个视图中显示一个项目。我想要实现的是将数据填充到轮播的 6 项中,如果有第 7 项,则转到轮播视图的下一页。那么我怎样才能实现这一目标呢?如果需要列表列表,我该怎么做?任何帮助表示赞赏。
查看完整描述

1 回答

?
慕标琳琳

TA贡献1830条经验 获得超9个赞

实际上,您不需要使用List of List。设置每个Frame的大小(宽度等于屏幕宽度的1/2,高度等于屏幕高度的1/3)。并设置属性 Span="3"


CarouselView在 Xamarin.Forms 4.0 之后发布。


<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"

             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"

             xmlns:d="http://xamarin.com/schemas/2014/forms/design"

             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

             mc:Ignorable="d"

             x:Name="contentPage"   // set the name of the contentpage

             x:Class="xxx.MainPage">

<CarouselView  ItemsSource="{Binding MyItems}"  BackgroundColor="Transparent" HorizontalOptions="Center" VerticalOptions="Center">

        <CarouselView.ItemsLayout>

            <GridItemsLayout SnapPointsAlignment="Center"  SnapPointsType="Mandatory" Span="3" Orientation="Horizontal"/>

        </CarouselView.ItemsLayout>

        <CarouselView.ItemTemplate>

            <DataTemplate>

                <Frame WidthRequest="{Binding Source={x:Reference contentPage}, Path=BindingContext.FrameWidth}" HeightRequest="{Binding Source={x:Reference contentPage}, Path=BindingContext.FrameHeight}" HasShadow="False" HorizontalOptions="FillAndExpand" IsClippedToBounds="True"  BackgroundColor="#4D2F4F4F" BorderColor="#294145">


                    <StackLayout HorizontalOptions="FillAndExpand">

                        <Grid >

                            <Grid.RowDefinitions>

                                <RowDefinition Height="Auto"/>

                                <RowDefinition Height="Auto"/>

                                <RowDefinition Height="Auto"/>

                                <RowDefinition Height="Auto"/>

                            </Grid.RowDefinitions>

                            <BoxView Grid.Row="0" Margin="2,2,10,2" HeightRequest="1" Color="LightGreen"></BoxView>

                            <Label Text="111111" Grid.Row="1" HorizontalOptions="StartAndExpand" FontSize="Small" TextColor="LightGray" Margin="2,0,0,0" >

                            </Label>

                            <Label Text="153" TextColor="White" HorizontalOptions="StartAndExpand" FontSize="Medium" Grid.Row="2" Margin="2,0,0,0" >

                            </Label>

                            <Image Source="alllead.png" HorizontalOptions="EndAndExpand" HeightRequest="30" Grid.Row="3" Margin="0,0,5,0"></Image>

                        </Grid>

                    </StackLayout>

                </Frame>

            </DataTemplate>

        </CarouselView.ItemTemplate>

</CarouselView>

在共享项目 App.xaml.cs 中

public static double ScreenWidth;

public static double ScreenHeight;

在 Android MainActivity.cs 中

protected override void OnCreate(Bundle savedInstanceState)

{

   TabLayoutResource = Resource.Layout.Tabbar;

   ToolbarResource = Resource.Layout.Toolbar;


   base.OnCreate(savedInstanceState);


   Xamarin.Essentials.Platform.Init(this, savedInstanceState);

            

   Forms.SetFlags("CollectionView_Experimental");

   global::Xamarin.Forms.Forms.Init(this, savedInstanceState);


   App.ScreenWidth = Resources.DisplayMetrics.WidthPixels/Resources.DisplayMetrics.Density; 

   App.ScreenHeight =Resources.DisplayMetrics.HeightPixels/Resources.DisplayMetrics.Density; 


   LoadApplication(new App());

}

在 iOS 中

public override bool FinishedLaunching(UIApplication app, NSDictionary options)

{

    //...

    App.ScreenWidth = UIScreen.MainScreen.Bounds.Width;

    App.ScreenHeight = UIScreen.MainScreen.Bounds.Height;

    //...

}


在代码隐藏或 ViewModel 中

public double FrameHeight { get; private set; }

public double FrameWidth { get; private set; }


//...


FrameHeight = App.ScreenHeight/3.0;

FrameWidth = App.ScreenWidth/2.0;


查看完整回答
反对 回复 2023-09-24
  • 1 回答
  • 0 关注
  • 52 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信