我正在编写在 ListView 中显示用户评论的 Xamarin 表单应用程序。
问题是它在 Android 上显示正确,但在 iOS 上它是开箱即用的(如屏幕截图所示)
这些项目是在运行异步网络请求收集信息后添加的,我不知道这是否会影响它。
安卓屏幕
iOS 屏幕
我的 XAML
<local:PostListView x:Name="MessageView" HasUnevenRows="True" IsPullToRefreshEnabled="True" Refreshing="MessageView_Refreshing" SeparatorVisibility="None" BackgroundColor="#7ed6df">
<ListView.ItemTemplate>
<DataTemplate>
<local:PostViewCell>
<StackLayout x:Name="MessageLayout" BackgroundColor="White" Margin="10, 10, 10, 10" Padding="10, 10, 15, 10">
<Image Source="options_icon.png" HeightRequest="18" HorizontalOptions="End" Margin="5, 0, 5, 0" IsVisible="{Binding ShowBanners}">
<Image.GestureRecognizers>
<TapGestureRecognizer Command="{Binding OptionClick}" CommandParameter="{Binding .}"/>
</Image.GestureRecognizers>
</Image>
<Label Text="{Binding Body}" HorizontalOptions="CenterAndExpand" TextColor="{Binding BodyColor}" FontSize="15" Margin="0, 10, 0, 10"/>
<StackLayout x:Name="MessageFooter" Orientation="Horizontal" IsVisible="{Binding ShowBanners}">
<Image x:Name="LikeSource" Source="{Binding LikeImageSource}" HeightRequest="20" HorizontalOptions="StartAndExpand" Margin="0, 0, 10, 0">
<Image.GestureRecognizers>
<TapGestureRecognizer Command="{Binding LikeClick}" CommandParameter="{Binding .}"/>
</Image.GestureRecognizers>
</Image>
<Label Text="{Binding Timestamp}" TextColor="Black" FontSize="10" HorizontalOptions="EndAndExpand"/>
</StackLayout>
</StackLayout>
</local:PostViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</local:PostListView>
PostViewCell.cs
using SocialNetwork.iOS.Renderers;
using SocialNetwork.Renderers;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
[assembly: ExportRenderer(typeof(PostViewCell), typeof(PostViewCelliOS))]
namespace SocialNetwork.iOS.Renderers
{
public class PostViewCelliOS : ViewCellRenderer
{
public override UIKit.UITableViewCell GetCell(Cell item, UIKit.UITableViewCell reusableCell, UIKit.UITableView tv)
{
var cell = base.GetCell(item, reusableCell, tv);
cell.SelectionStyle = UITableViewCellSelectionStyle.None;
return cell;
}
}
}
最佳答案
您可以尝试创建一个 StackLayout 包装您的内容,如:
<local:PostViewCell>
<StackLayout>
<StackLayout x:Name="MessageLayout" BackgroundColor="White" Margin="10, 10, 10, 10" Padding="10, 10, 15, 10">
<Image Source="options_icon.png" HeightRequest="18" HorizontalOptions="End" Margin="5, 0, 5, 0" IsVisible="{Binding ShowBanners}">
<Image.GestureRecognizers>
<TapGestureRecognizer Command="{Binding OptionClick}" CommandParameter="{Binding .}"/>
</Image.GestureRecognizers>
</Image>
<Label Text="{Binding Body}" HorizontalOptions="CenterAndExpand" TextColor="{Binding BodyColor}" FontSize="15" Margin="0, 10, 0, 10"/>
<StackLayout x:Name="MessageFooter" Orientation="Horizontal" IsVisible="{Binding ShowBanners}">
<Image x:Name="LikeSource" Source="{Binding LikeImageSource}" HeightRequest="20" HorizontalOptions="StartAndExpand" Margin="0, 0, 10, 0">
<Image.GestureRecognizers>
<TapGestureRecognizer Command="{Binding LikeClick}" CommandParameter="{Binding .}"/>
</Image.GestureRecognizers>
</Image>
<Label Text="{Binding Timestamp}" TextColor="Black" FontSize="10" HorizontalOptions="EndAndExpand"/>
</StackLayout>
</StackLayout>
</StackLayout>
</local:PostViewCell>
关于xaml - ListView 在 iOS 上无法正确显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48729290/