简体   繁体   English

Xamarin.Forms(iOS项目):ListView分隔符显示在所有屏幕中

[英]Xamarin.Forms (iOS Project): ListView Separator shows in all screen

I have a ListView with the default SeparatorVisibility. 我有一个带有默认SeparatorVisibility的ListView。 My Android project shows the Separator if there are elements in the ItemsSource and stops showing it below the last element. 如果ItemsSource中有元素,我的Android项目将显示Separator,并在最后一个元素下方停止显示它。 It's the result I want for my iOS project. 这是我想要的iOS项目的结果。

However, in my iOS project the screen is full of Separators no matter how many elements I have, even if I have no elements or only one, the Separators still being there. 但是,在我的iOS项目中,无论我有多少元素,屏幕上都充满了分隔符,即使我没有元素或只有一个,分隔符仍然存在。

Could someone give me a reason and how to fix it please? 有人可以给我一个原因,如何解决? Thanks. 谢谢。

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;
            }
        }
    }
}

I think you can take a look to this post 我想你可以看一下这篇文章

these are some tips 这些是一些技巧

First disable the default separator, this is done by adding following property to the ListView XAML 首先禁用默认分隔符,这是通过将以下属性添加到ListView XAML来完成的

SeparatorColor="Transparent"

After this, wrap the complete ViewCell content inside a double StackLayout! 之后,将完整的ViewCell内容包装在一个Double StackLayout中! I know this sounds like overkill but this way you'll not run into any BoxView issues regarding margins inside the ViewCell… or other stuff. 我知道这听起来有些矫kill过正,但是这样您就不会遇到任何有关ViewCell…或其他内容的边距的BoxView问题。 The first StackLayout should have a BackgroundColor set to the colour you want your separator to be, the second StackLayout should have the same BackgroundColor as the rest of the container it is in… in our example the page and that is set to white. 第一个StackLayout应该将BackgroundColor设置为您希望分隔符使用的颜色,第二个StackLayout应该与其所在容器的其余部分具有相同的BackgroundColor……在我们的示例页面中,该颜色设置为白色。 Be sure to also add a Margin to the bottom of this second StackLayout because that will represent the thickness of our separator! 还要确保在第二个StackLayout的底部添加边距,因为这将代表分隔符的厚度!

I think you can play with this "Margin"... when your data is empty, remove the margin so you should not have the separator 我认为您可以使用此“边距” ...当您的数据为空时,请删除边距,这样就不必使用分隔符

<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>

With that in place, you'll get the same visual result as the preview image at the top right of this blog post. 将其放置在适当位置后,您将获得与本博客文章右上方的预览图像相同的视觉效果。

As a bonus, you could omit one of the StackLayouts IF your page has a background color other than white. 另外,如果页面的背景颜色不是白色,则可以省略其中的StackLayouts。 Because if this is the case, you can use that color as the separator color by playing with transparency inside the ListView. 因为如果是这种情况,则可以通过在ListView中播放透明度来使用该颜色作为分隔符颜色。

Example of this, note will only work if the page itself also has a BackgroundColor set to Olive! 例如,仅在页面本身也将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>

That is due to the implementation of the ListView on the iOS side. 那是由于在iOS端实现了ListView It renders as a UITableView which (in the case of the ListView ) always takes the entire height and shows empty items. 它呈现为UITableView (对于ListView )始终占据整个高度并显示空项目。 To change this type of behavior you could set the ListView height dynamically in the code-behind for your page. 要更改这种类型的行为,您可以在页面的代码隐藏区中动态设置ListView高度。 Another option is taking a look at Xamarin's Evolve sample app which has a few pages where it uses a CardView combined with a ListView to make it appear as it does on Android. 另一个选择是看一下Xamarin的Evolve示例应用程序 ,该应用程序有几个页面,其中使用CardViewListView结合使用,使其看起来像在Android上一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM