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;
- 1 回答
- 0 关注
- 93 浏览
添加回答
举报