Xamarin使用Xamarin.Community.Toolkit实现的Popup纵向过长占满全屏如何修复
问题修复方案
- 根本原因:Xamarin.Community.Toolkit的Popup控件默认未设置尺寸约束时,会自动占用尽可能多的可用屏幕空间,同时代码中Label使用了
CenterAndExpand属性,也会额外占用纵向冗余空间,共同导致弹窗纵向过长占满全屏。 - 修复步骤:
- 给
<xct:Popup>标签添加显式尺寸和布局约束属性,指定弹窗的宽高、居中显示 - 将Grid第一行Label的
VerticalOptions属性从CenterAndExpand修改为Center,取消多余空间占用
修改后的完整代码如下:
<xct:Popup xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:xct="http://xamarin.com/schemas/2020/toolkit" x:Class="Barcode_Mobile.Views.ConnectionPage" Size="350, 360" VerticalOptions="Center" HorizontalOptions="Center"> <Grid Padding="20,10,20,10" > <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="50" /> <RowDefinition Height="50" /> <RowDefinition Height="50" /> <RowDefinition Height="50" /> <RowDefinition Height="50" /> </Grid.RowDefinitions> <Label Text="Server And Database Informations" FontSize="18" VerticalOptions="Center" HorizontalOptions="Start" Grid.Row="0"/> <Entry Placeholder="Example: 127.0.0.1" Grid.Row="1" x:Name="TxtServerAddress"/> <Entry Placeholder="Username" Grid.Row="2" x:Name="TxtLogIn"/> <Entry Placeholder="Password" Grid.Row="3" x:Name="TxtPasswd" IsPassword="True"/> <Picker Grid.Row="4" x:Name="DbPicker" ItemsSource="{Binding}" Visual="Material"/> <Button Text="Connect" Grid.Row="5" x:Name="BtnConnect" Clicked="BtnConnect_Clicked" WidthRequest="300"/> </Grid> </xct:Popup>
- 附加注意事项:
- 你可以根据实际显示效果调整
Size属性的宽高数值,也可以单独用WidthRequest和HeightRequest分别设置宽高 - 如需适配不同屏幕尺寸,可根据设备屏幕宽高按比例设置弹窗尺寸,避免不同设备显示异常
- 若修改属性后仍存在尺寸异常,可将Xamarin.Community.Toolkit升级到最新稳定版,部分旧版本存在Popup尺寸计算的已知bug
- 你可以根据实际显示效果调整
内容的提问来源于stack exchange,提问作者Hainn
相关产品推荐
相关产品推荐

