.NET MAUI中RefreshView内ScrollView在iOS调用InvalidateMeasure不重置高度
MAUI ScrollView下拉刷新后iOS端高度未收缩的问题
当下拉刷新移除记录后,ScrollView未根据新内容长度调整(收缩)高度,尽管已经调用了(svMain as IView).InvalidateMeasure();
注意:这段代码在Android上运行正常,但在iOS上无效。
复现步骤
- 预加载10条数据
- 点击“Add 10”按钮
- 从顶部下拉触发刷新
ReportsPage.xaml
<?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="testrv.ReportsPage" Title="ReportsPage"> <Grid x:Name="gridMain"> <Grid.RowDefinitions> <RowDefinition Height="*"></RowDefinition> </Grid.RowDefinitions> <RefreshView x:Name="rvMain" Grid.Row="0" IsRefreshing="{Binding IsRefreshing}" Command="{Binding RefreshCommand}" RefreshColor="{DynamicResource Primary}"> <ScrollView x:Name="svMain"> <Grid Margin="20"> <Grid.RowDefinitions> <RowDefinition Height="50" x:Name="gridMain_row_0"></RowDefinition> <RowDefinition Height="*" x:Name="gridMain_row_1"></RowDefinition> <RowDefinition Height="60" x:Name="gridMain_row_2"></RowDefinition> </Grid.RowDefinitions> <VerticalStackLayout Grid.Row="0" x:Name="vslHeader" Spacing="10"></VerticalStackLayout> <VerticalStackLayout Grid.Row="1" x:Name="vslBody" Spacing="10" VerticalOptions="Start"> <Button Clicked="Button_Clicked" Text="Add 10"></Button> <VerticalStackLayout x:Name="vslFeed" Spacing="10" BindableLayout.ItemsSource="{Binding Labels}"> <BindableLayout.ItemTemplate> <DataTemplate> <Label Text="{Binding .}" BackgroundColor="Azure"></Label> </DataTemplate> </BindableLayout.ItemTemplate> </VerticalStackLayout> </VerticalStackLayout> <VerticalStackLayout Grid.Row="2" x:Name="vslFooter" Spacing="10"></VerticalStackLayout> </Grid> </ScrollView> </RefreshView> </Grid> </ContentPage>
ReportsPage.xaml.cs
using System.Collections.ObjectModel; using System.Windows.Input; namespace testrv; public partial class ReportsPage : ContentPage { ObservableCollection<string> Labels = new ObservableCollection<string>(); public ReportsPage() { InitializeComponent(); BindableLayout.SetItemsSource(vslFeed, Labels); } protected async override void OnAppearing() { base.OnAppearing(); await SetLabels(); ICommand refreshCommand = new Command(async () => { // IsRefreshing is true rvMain.IsRefreshing = true; await SetLabels(); rvMain.IsRefreshing = false; (svMain as IView).InvalidateMeasure(); }); rvMain.Command = refreshCommand; } private async Task SetLabels() { Labels.Clear(); for (int i = 0; i < 10; i++) { AddLabel(); } } private void AddLabel() { Labels.Add("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam feugiat tincidunt lacinia. " + "Fusce at magna et odio finibus consectetur vel vel velit. Integer eu felis vel lorem efficitur semper sed sagittis justo. " + "Mauris hendrerit ex semper augue posuere ornare. Vestibulum rhoncus commodo risus, id interdum nibh dictum nec."); } private void Button_Clicked(object sender, EventArgs e) { for (int i = 0; i < 10; i++) { AddLabel(); } } }
内容的提问来源于stack exchange,提问作者aberdat
相关产品推荐
相关产品推荐

