Xamarin Forms:如何在 ListView 周围添加边框?

Xamarin Forms: How to add a border around a ListView?

我正在尝试在 Xamarin.Forms 附带的标准 ListView 组件周围添加一个 1px 的边框,而 BorderColor 属性 似乎不存在。

如何实现?

您可以使用填充使框架充当边框。记得关掉阴影。

<Frame BackgroundColor="Black" Padding="1" CornerRadius="0" HasShadow="False">
        <!-- Your ListView here... make sure to set a BackgroundColor -->
</Frame>

您也可以使用 BoxView。并使用 RowHeight 属性 设置 HeightRequest 属性 以删除列表视图底部的空 space。

Xaml:

   <Grid Padding="5">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"></RowDefinition>
        </Grid.RowDefinitions>
        <BoxView BackgroundColor="Green" HeightRequest="5"></BoxView>

        <ListView x:Name="listview" BackgroundColor="White" RowHeight="40" ItemsSource="{Binding Names}" Margin="5" >
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <Label Text="{Binding Name}"></Label>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Grid>

后面的代码:

 public partial class Page5 : ContentPage
{
    public ObservableCollection<Info> Names { get; set; }
    public Page5()
    {
        InitializeComponent();
        Names = new ObservableCollection<Info>()
        {
            new Info(){ Name="A"},
            new Info(){ Name="B"},
            new Info(){ Name="C"},
            new Info(){ Name="D"},
            new Info(){ Name="E"},
            new Info(){ Name="F"},
            new Info(){ Name="G"},
        };
        listview.HeightRequest = listview.RowHeight * Names.Count;
        this.BindingContext = this;
    }
}
public class Info
{
    public string Name { get; set; }
}