求助:.NET MAUI Android端Entry控件显示异常问题及临时解决方案
.NET MAUI Entry控件在Android 10设备上不显示的问题及临时解决方法
问题场景
使用VS Community 2022 Preview v17.3.0 Preview 4.0开发.NET MAUI应用,以下XAML代码在Windows模拟器运行完全正常,但连接Motorola moto g(7) power(Android 10.0 API29)本地设备时,第二个用于输入电话号码的Entry控件完全不显示——仅能看到“Phone:”标签,控件无法点击激活,甚至连设置的AliceBlue背景都不展示。
问题代码
<VerticalStackLayout Margin="20"> <HorizontalStackLayout Margin="10"> <Label Text="Name:" FontSize="Large" VerticalOptions="Center" WidthRequest="160" HorizontalOptions="End" /> <Entry x:Name="MyStoreDetailsNameEntry" Text="{Binding Name, Mode=TwoWay}" BackgroundColor="AliceBlue" FontSize="Large" VerticalOptions="Center" HorizontalOptions="Start"> <Entry.Behaviors> <toolkit:EventToCommandBehavior x:DataType="viewmodels:StoreDetailsViewModel" EventName="Completed" Command="{Binding Path=BindingContext.StoreDetailsNameCompletedCommand, Source={Reference myStoreDetailsPage}}" CommandParameter="{Reference MyStoreDetailsNameEntry}"/> </Entry.Behaviors> </Entry> </HorizontalStackLayout> <HorizontalStackLayout Margin="10"> <Label Text="Phone:" FontSize="Large" VerticalOptions="Center" WidthRequest="160" HorizontalOptions="End" /> <Entry x:Name="MyStoreDetailsPhoneEntry" Text="{Binding Phone, Mode=TwoWay}" BackgroundColor="AliceBlue" FontSize="Large" VerticalOptions="Center" HorizontalOptions="Start"> <Entry.Behaviors> <toolkit:EventToCommandBehavior x:DataType="viewmodels:StoreDetailsViewModel" EventName="Completed" Command="{Binding Path=BindingContext.StoreDetailsPhoneCompletedCommand, Source={Reference myStoreDetailsPage}}" CommandParameter="{Reference MyStoreDetailsPhoneEntry}"/> </Entry.Behaviors> </Entry> </HorizontalStackLayout> </VerticalStackLayout>
临时解决方案(2022年8月12日更新)
进入项目的Resources/Styles/Styles.xaml文件,找到TargetType为Entry的Style代码块,插入以下Setter配置:
<Setter Property = "MinimumWidthRequest" Value="50" />
修改后的完整Entry样式代码:
<Style TargetType="Entry"> <Setter Property="TextColor" Value="{AppThemeBinding Light={StaticResource Black}, Dark={StaticResource White}}" /> <Setter Property="BackgroundColor" Value="Transparent" /> <Setter Property="FontFamily" Value="OpenSansRegular"/> <Setter Property="FontSize" Value="14" /> <Setter Property="PlaceholderColor" Value="{AppThemeBinding Light={StaticResource Gray200}, Dark={StaticResource Gray500}}" /> <Setter Property="MinimumWidthRequest" Value="50" /> <Setter Property="VisualStateManager.VisualStateGroups"> <VisualStateGroupList> <VisualStateGroup x:Name="CommonStates"> <VisualState x:Name="Normal" /> <VisualState x:Name="Disabled"> <VisualState.Setters> <Setter Property="TextColor" Value="{AppThemeBinding Light={StaticResource Gray300}, Dark={StaticResource Gray600}}" /> </VisualState.Setters> </VisualState> </VisualStateGroup> </VisualStateGroupList> </Setter> </Style>
此方案来自官方.NET Conf相关技术视频,仅作为临时解决手段使用,希望官方后续能彻底修复该适配问题。同时建议修改.NET MAUI应用的脚手架代码,在创建新项目时自动将MinimumWidthRequest的Setter配置添加到默认样式中,从源头避免开发者遇到此类问题。
内容的提问来源于stack exchange,提问作者T. A. Pfaff
相关产品推荐
相关产品推荐

