Xamarin.Forms文字偶发消失求助:热重载后恢复无报错
Xamarin.Forms 偶发文字消失问题排查与解决
问题现象
- 偶发部分文字莫名消失,无设备/Android版本依赖,触发规律不明确
- 调试阶段无任何报错信息,按下
Ctrl+S触发热重载后文字可恢复正常
问题截图


相关代码
页面XAML代码
<StackLayout BindableLayout.ItemsSource="{Binding EDocuments}"> <BindableLayout.ItemTemplate> <DataTemplate> <SwipeView Grid.Row="0" BackgroundColor="Transparent"> <SwipeView.LeftItems> <SwipeItems> <SwipeItem Text="Акти" IconImageSource="{extension:ImageResource DriverHelper2.Resources.Icons.eActFttn.png}" BackgroundColor="Transparent" Command="{Binding Source={RelativeSource AncestorType={x:Type view:EdocumentsListViewModel}}, Path=ETTNsActsCommand}" CommandParameter="{Binding Id}"> </SwipeItem> </SwipeItems> </SwipeView.LeftItems> <Frame CornerRadius="10" BackgroundColor="#66000000"> <StackLayout Orientation="Vertical" HorizontalOptions="FillAndExpand"> <StackLayout Orientation="Horizontal"> <Label Style="{StaticResource LabelTitleFontSize}" Text="Номер документа:" ></Label> <Label Style="{StaticResource LabelFontSize}" TextColor="White" Text="{Binding DocumentNum}"></Label> </StackLayout> <StackLayout Orientation="Horizontal"> <Label Style="{StaticResource LabelTitleFontSize}" Text="Точка вивантаження:" ></Label> <Label Style="{StaticResource LabelFontSize}" TextColor="White" Text="{Binding UnloadPoint}"></Label> </StackLayout> <StackLayout Orientation="Horizontal"> <Label Style="{StaticResource LabelTitleFontSize}" Text="Кількість місць:" ></Label> <Label Style="{StaticResource LabelFontSize}" TextColor="White" Text="{Binding PlacesTotal}"></Label> </StackLayout> <StackLayout Orientation="Horizontal"> <Label Style="{StaticResource LabelTitleFontSize}" Text="Загальна вага:" ></Label> <Label Style="{StaticResource LabelFontSize}" TextColor="White" Text="{Binding WeightTotal}"></Label> </StackLayout> <StackLayout Orientation="Horizontal"> <Label Style="{StaticResource LabelTitleFontSize}" Text="Статус:" ></Label> <Label Style="{StaticResource LabelTitleFontSize}" TextColor="White" Text="{Binding EDocState.StateName}"></Label> </StackLayout> <StackLayout Orientation="Horizontal"> <Label Text="Перелік товару:" VerticalTextAlignment="Center" TextColor="Yellow" FontSize="22"/> <Image WidthRequest="50" HeightRequest="50" HorizontalOptions="EndAndExpand" Source="{Binding HideImagePath, Converter={StaticResource StringToImage}}"> <Image.GestureRecognizers> <TapGestureRecognizer Command="{Binding HideEDocs}" NumberOfTapsRequired="1" /> </Image.GestureRecognizers> </Image> </StackLayout> <StackLayout BindableLayout.ItemsSource="{Binding Specifications}" IsVisible="{Binding IsHide}"> <BindableLayout.ItemTemplate> <DataTemplate> <Frame CornerRadius="5" OutlineColor="#c3c3c3" BackgroundColor="White"> <StackLayout> <StackLayout Orientation="Horizontal" HorizontalOptions="FillAndExpand"> <Label Style="{StaticResource LabelTitleFontSize}" Text="Найменування:"/> <Label Style="{StaticResource LabelFontSize}" Text="{Binding Name}"/> </StackLayout> <StackLayout Orientation="Horizontal" HorizontalOptions="FillAndExpand"> <Label Style="{StaticResource LabelTitleFontSize}" Text="Тип:"/> <Label Style="{StaticResource LabelFontSize}" Text="{Binding TypeOfPackaging}"/> </StackLayout> <StackLayout Orientation="Horizontal" HorizontalOptions="FillAndExpand"> <Label Style="{StaticResource LabelTitleFontSize}" Text="Вага:"/> <Label Style="{StaticResource LabelFontSize}" Text="{Binding GrossWeight}"/> </StackLayout> <StackLayout Orientation="Horizontal" HorizontalOptions="FillAndExpand"> <Label Style="{StaticResource LabelTitleFontSize}" Text="Кількість місць:"/> <Label Style="{StaticResource LabelFontSize}" Text="{Binding NumberOfSeats}"/> </StackLayout> </StackLayout> </Frame> </DataTemplate> </BindableLayout.ItemTemplate> </StackLayout> <StackLayout Orientation="Horizontal" Padding="0, 15, 0, 0"> <Label Style="{StaticResource LabelTitleFontSize}" TextColor="Yellow" Text="Вибрати:" /> <Switch Scale="1.5" IsToggled="{Binding IsChecked}" OnColor="LightGray" HorizontalOptions="EndAndExpand" ThumbColor="{Binding SwitchColor}" /> </StackLayout> </StackLayout> </Frame> </SwipeView> </DataTemplate> </BindableLayout.ItemTemplate> </StackLayout>
全局Label字体设置(App.xaml)
<Application.Resources> <ResourceDictionary> <local:StringToImageSourceConverter x:Key="StringToImage" /> <local:ImageResource x:Key="ImageResource" /> <Style TargetType="Label"> <Setter Property="FontFamily" Value="BanderaFont" /> </Style> </ResourceDictionary> </Application.Resources>
AssemblyInfo.cs字体导出代码
[assembly: ExportFont("BanderaProMedium.ttf", Alias = "BanderaFont")]
解决方向
自定义字体加载优化
- 尝试在全局Label样式中显式指定字体完整标识,避免隐式加载异常:
<Style TargetType="Label"> <Setter Property="FontFamily" Value="BanderaProMedium.ttf#BanderaProMedium" /> </Style> - 确认字体文件的生成操作设置为
Embedded resource,确保打包时不会丢失。
- 尝试在全局Label样式中显式指定字体完整标识,避免隐式加载异常:
手动触发视图刷新
- 在ViewModel的集合或属性更新后,手动调用
OnPropertyChanged通知视图重绘:// 示例:EDocuments集合更新后触发 OnPropertyChanged(nameof(EDocuments));
- 在ViewModel的集合或属性更新后,手动调用
升级Xamarin.Forms版本
- 此类偶发渲染bug在旧版本中较为常见,尝试升级到最新稳定版(如5.0.0.2515及以上),官方可能已修复类似异常。
减少布局嵌套层级
- 过多嵌套Frame和StackLayout可能引发渲染性能问题,尝试将外层Frame替换为ContentView,或简化布局结构。
内容的提问来源于stack exchange,提问作者Winteriscoming
相关产品推荐
相关产品推荐

