【问题标题】:How to set margin around ImageCell in Xamarin.Forms?如何在 Xamarin.Forms 中围绕 ImageCell 设置边距?
【发布时间】:2017-10-30 19:49:18
【问题描述】:

我想在 ListView 的 Image 单元格项目周围留出一些空间:

<StackLayout>
    <ListView ItemsSource="{Binding Items}" HasUnevenRows="True">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ImageCell Text="{Binding Title}" ImageSource="{Binding Image}" TextColor="Black" DetailColor="Gray"/>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</StackLayout>

但是ImageCell 没有可用的 Margin 属性?

编辑

在发布问题之前,我按照 cvanbeek 的建议做了:

<DataTemplate>
   <StackLayout Padding="5">
       <ImageCell Text="{Binding Title}" ImageSource="{Binding Image}" TextColor="Black" DetailColor="Gray"/>
   </StackLayout>
</DataTemplate>

但我得到了这个例外:

未处理的异常:

System.InvalidCastException:指定的强制转换无效。

在 MainActivity.cs 的 OnCreate 方法中

【问题讨论】:

  • 你可以用带有填充的StackLayout包围它
  • 不可能,请参阅编辑

标签: xaml xamarin xamarin.ios xamarin.forms xamarin.android


【解决方案1】:

DataTemplate definition

只有View 可以放在DataTemplate 中,但是StackLayoutLayout

改用ViewCell,更灵活可控。

<ListView >
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <StackLayout Padding="20" Orientation="Horizontal" >
                    <Label Margin="20" Text="123"/>
                    <Image Source="Assets/StoreLogo.png"/>
                </StackLayout>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

【讨论】:

    猜你喜欢
    • 2017-07-23
    • 2020-08-11
    • 1970-01-01
    • 2011-05-19
    • 2022-10-21
    • 2016-08-24
    • 2012-09-16
    • 2017-12-15
    • 1970-01-01
    相关资源
    最近更新 更多