【问题标题】:Xamarin.Forms (iOS Project): ListView Separator shows in all screenXamarin.Forms(iOS 项目):ListView 分隔符显示在所有屏幕中
【发布时间】:2017-10-06 08:03:18
【问题描述】:

我有一个具有默认 SeparatorVisibility 的 ListView。如果 ItemsSource 中有元素,我的 Android 项目会显示分隔符,并在最后一个元素下方停止显示它。这是我想要的 iOS 项目的结果。

但是,在我的 iOS 项目中,无论我有多少元素,屏幕上都充满了分隔符,即使我没有元素或只有一个,分隔符仍然存在。

谁能给我一个理由以及如何解决它?谢谢。

【问题讨论】:

    标签: ios listview xamarin.forms


    【解决方案1】:

    我想你可以去this post看看

    这些是一些提示

    首先禁用默认分隔符,这是通过向 ListView XAML 添加以下属性来完成的

    SeparatorColor="Transparent"
    

    在此之后,将完整的 ViewCell 内容包装在双 StackLayout 中!我知道这听起来有点矫枉过正,但这样你就不会遇到任何关于 ViewCell 内边距的 BoxView 问题……或其他东西。 第一个 StackLayout 的 BackgroundColor 应该设置为您希望分隔符的颜色,第二个 StackLayout 应该具有与其所在容器的其余部分相同的 BackgroundColor……在我们的示例中,页面设置为白色。请务必在第二个 StackLayout 的底部添加一个 Margin,因为这将代表我们分隔符的厚度!

    我认为您可以使用此“边距”...当您的数据为空时,请删除边距,这样您就不应该有分隔符

    <ListView x:Name="SeparatorListView"
        SeparatorColor="Transparent"
        ItemsSource="{Binding Persons}"
        Margin="0,20,0,0"
        RowHeight="60"
        HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"
        BackgroundColor="White"
        Grid.Row="1">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell IsEnabled="false">
                    <StackLayout VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"
                        BackgroundColor="Black">
                    <StackLayout VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"
                        BackgroundColor="White"
                        Margin="0,0,0,0.4">
                        <StackLayout VerticalOptions="CenterAndExpand" HorizontalOptions="FillAndExpand" Spacing="0">
                            <Label Text="{Binding FullName}" TextColor="Maroon" VerticalOptions="CenterAndExpand" Margin="20,0,20,0" />
                            <Label Text="{Binding Profession}" TextColor="Maroon" VerticalOptions="CenterAndExpand" Margin="20,0,20,0" />
                        </StackLayout>
                    </StackLayout>
                    </StackLayout>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
    

    设置好之后,您将获得与这篇博文右上角的预览图相同的视觉效果。

    作为奖励,如果您的页面的背景颜色不是白色,您可以省略其中一个 StackLayouts。因为如果是这种情况,您可以通过在 ListView 中使用透明度来使用该颜色作为分隔符颜色。

    此示例,仅当页面本身的 BackgroundColor 也设置为 Olive 时才有效!

    <ListView x:Name="SeparatorListView"
        SeparatorColor="Transparent"
        ItemsSource="{Binding Persons}"
        Margin="0,20,0,0"
        RowHeight="60"
        HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"
        BackgroundColor="Olive"
        Grid.Row="1">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell IsEnabled="false">
                        <StackLayout BackgroundColor="#f4eac3"
                                    Padding="0,5,0,5"
                                     HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
                            <StackLayout BackgroundColor="Transparent"
                                         HorizontalOptions="FillAndExpand" VerticalOptions="CenterAndExpand"
                                         Spacing="0"
                                         Margin="20,0,20,0">
                            <Label Text="{Binding FullName}" TextColor="Maroon" VerticalOptions="CenterAndExpand" />
                            <Label Text="{Binding Profession}" TextColor="Maroon" VerticalOptions="CenterAndExpand" />
                        </StackLayout>
                    </StackLayout>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
    

    【讨论】:

    • 感谢@AlessandroCaliaro!它对我不起作用,因为我的 Page 有一个 BackgroundImage,而我的 ListView 有 BackgroundColor="Transparent",因此,我无法做到这一点。但是你给了我一个想法,让我的SeparatorVisibility="None" 并添加到我的网格(我在每个 ViewCell 中都有一个)一个额外的小行,其背景颜色模拟我的分隔符!效果很好,非常感谢!
    【解决方案2】:
    using UIKit;
    using Xamarin.Forms;
    using Xamarin.Forms.Platform.iOS;
    
    [assembly: ExportRenderer (typeof(ListView), typeof(CustomListViewRenderer))]
    namespace yourNamespace
    {
        public class CustomListViewRenderer : ListViewRenderer
        {
            protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.ListView> e)
            {
                base.OnElementChanged(e);
                if (e.NewElement != null)
                {
                    var listView = Control as UITableView;
                    listView.SeparatorInset = UIEdgeInsets.Zero;
                }
            }
        }
    }
    

    【讨论】:

    • 据我所知,这个渲染器不会删除多余的分隔线。至少在 iOS 14 上没有。
    【解决方案3】:

    这是由于在 iOS 端实现了ListView。它呈现为UITableView(在ListView 的情况下)始终占据整个高度并显示空项目。要更改这种类型的行为,您可以在页面的代码隐藏中动态设置 ListView 高度。另一种选择是查看 Xamarin 的 Evolve sample app,它有几个页面使用 CardView 结合 ListView 使其看起来像在 Android 上一样。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多