.NET 8 MAUI中ImageButton的FontImageSource图标适配问题求助
问题现象
将MAUI安卓/iOS项目升级至.NET 8后,ImageButton出现以下渲染异常:
- .NET 7中无需设置宽高,按钮尺寸会自动适配内边距,图标可完整显示且不被
CornerRadius裁切 - .NET 8中必须指定
WidthRequest和HeightRequest才能正常显示,但设置后内边距失效;同时FontImageSource图标渲染尺寸变大,即使调整图标Size、按钮宽高或圆角半径,仍会被圆角裁切,无法还原.NET 7的显示效果
临时解决方法
官方已记录该问题,当前可行的临时修复方案是:在ImageButton上添加Aspect="Center"属性,强制图标居中适配按钮尺寸,避免被圆角裁切。
修改后的代码示例
<Grid Margin="0" BackgroundColor="Black" > <Grid.RowDefinitions> <RowDefinition Height="40" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <ImageButton Grid.Row="1" Margin="15,0,15,10" HorizontalOptions="End" VerticalOptions="End" HeightRequest="40" WidthRequest="40" Padding="5" CornerRadius="20" BackgroundColor="#32578f" Aspect="Center"> <!-- 添加该属性修复裁切问题 --> <ImageButton.Source> <FontImageSource Glyph="{x:Static m:MaterialOutlined.Grid_view}" FontFamily="MaterialOutlined" Color="White" Size="10"/> </ImageButton.Source> </ImageButton> </Grid>
内容的提问来源于stack exchange,提问作者Shaboboo
相关产品推荐
相关产品推荐

