Xamarin Forms Center CollectionView内容

问题描述 投票:0回答:1

我有问题。我创建了一个看起来像这样的页面:

enter image description here

现在,我希望CollectionView的内容居中。我已经尝试过在Horizo​​ntalOptions = Center上进行设置,但是没有运气!

这是该部分的代码:

<StackLayout HorizontalOptions="CenterAndExpand">
    <CollectionView ItemsSource="{Binding coinDataList}" ItemsLayout="HorizontalList" Margin="0" HorizontalOptions="CenterAndExpand" BackgroundColor="Red">
        <CollectionView.ItemTemplate>
            <DataTemplate>
                <Grid RowSpacing="0" Margin="20,0,20,0" HorizontalOptions="CenterAndExpand" Padding="0">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="20" />
                        <RowDefinition Height="5" />
                        <RowDefinition Height="20" />
                        <RowDefinition Height="10" />
                        <RowDefinition Height="20" />
                    </Grid.RowDefinitions>

                    <Label Grid.Row="0" VerticalOptions="CenterAndExpand" HorizontalOptions="Center" Text="{Binding Coin}" FontAttributes="Bold" TextColor="#00D8FF" FontSize="18"/>
                    <Label Grid.Row="2" VerticalOptions="CenterAndExpand" HorizontalOptions="Center" Text="{Binding Price, StringFormat='{0:F2}'}" TextColor="White" FontSize="18"/>
                </Grid>
            </DataTemplate>
        </CollectionView.ItemTemplate>
    </CollectionView>
</StackLayout>

我该怎么做?

xaml xamarin xamarin.forms xamarin.android xamarin.ios
1个回答
0
投票

您可以通过将BindableLayout与水平方向的StackLayout结合使用来实现。它的性能不如CollectionView,但是从您的UI来看,您似乎在ItemSource集合中没有很多项目,但是当您的UI较少时,您需要UI是动态的,均匀地分布和居中项目。随着项目列表的增加,UI看起来更像是水平列表视图。

因此这是最小的工作XAML,您可以对其进行修改以适合您的项目。为简单起见,我在XAML中添加了所有内容(没有代码),因此您可以将此XAML插入内容页面并进行测试!

<?xml version="1.0" encoding="utf-8" ?>
<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"
             xmlns:generic="clr-namespace:System.Collections.Generic;assembly=netstandard"
             mc:Ignorable="d"
             x:Class="Playground.MainPage">
    <ContentPage.Resources>
        <ResourceDictionary>
            <generic:List x:Key="SymbolNames" x:TypeArguments="x:String">
                <x:String>BTC</x:String>
                <x:String>LTC</x:String>
                <x:String>ETH</x:String>
                <x:String>OT1</x:String>
                <x:String>OT2</x:String>
                <x:String>OT3</x:String>
                <x:String>OT4</x:String>
                <x:String>OT5</x:String>
                <x:String>OT6</x:String>
            </generic:List>
        </ResourceDictionary>
    </ContentPage.Resources>

    <ScrollView Orientation="Horizontal" HeightRequest="60" VerticalOptions="Start">
        <StackLayout BindableLayout.ItemsSource="{Binding Source={StaticResource SymbolNames}}" Orientation="Horizontal">
            <BindableLayout.ItemTemplate>
                <DataTemplate>
                    <Label Text="{Binding}" HorizontalOptions="CenterAndExpand" VerticalOptions="Center" Margin="10"/>
                </DataTemplate>
            </BindableLayout.ItemTemplate>
        </StackLayout>
    </ScrollView>
</ContentPage>

UI当3个项目:

enter image description here

UI当有多个溢出项时:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.