Xamarin.iOS 创建 MAUI 项目,ScrollView 滚动条不工作的问题解决

布局文件如下:

<?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="MauiApp3.MainPage">

    <ScrollView x:Name="myScllView">
        <StackLayout
        x:Name="vtlStackLyt"
        Spacing="25"
        Padding="30,0"
        Background="red"
        VerticalOptions="Center">

            <Image
            Source="dotnet_bot.png"
            SemanticProperties.Description="Cute dot net bot waving hi to you!"
            HeightRequest="200"
            HorizontalOptions="Center" />
            <Image
            Source="dotnet_bot.png"
            SemanticProperties.Description="Cute dot net bot waving hi to you!"
            HeightRequest="200"
            HorizontalOptions="Center" />            

            <Label
            Text="Hello, World!"
            SemanticProperties.HeadingLevel="Level1"
            FontSize="32"
            HorizontalOptions="Center" />

            <Label
            x:Name="lblText"
            Text="{Binding MyLabelText}"
            SemanticProperties.HeadingLevel="Level2"
            SemanticProperties.Description="Welcome to dot net Multi platform App U I"
            FontSize="18"
            
            HorizontalOptions="Center" />

            <Button
            x:Name="CounterBtn"
            Text="Click me"
            SemanticProperties.Hint="Counts the number of times you click"
            Clicked="OnCounterClicked"
            HorizontalOptions="Center" />

        </StackLayout>

    </ScrollView>

</ContentPage>

上述布局文件是使用 VS 创建 iOS 平台下的 MAUI 项目自动生成的。在 cs 代码中对 Label lblText 赋值一个大文本以后,外层的 ScrollView 不能显示全部内容,也就是说 ScrollView 无法自适应子控件内容变化后的高度。代码如下:

private void OnCounterClicked(object sender, EventArgs e)
{
    // 主线程更新 ui
    MainThread.BeginInvokeOnMainThread(() =>
    {
        // 大文本,显示时会换行,超出原始高度
        strDemo = "Demo demo demo demo demo demo demo demo demo demo demo demo demo
 demo demo demo demo demo demo demo demo demo demo demo demo demo
 demo demo demo demo demo demo demo demo demo .............";

        // 把大文本字符串赋值给绑定对象,呈现在页面
        viewModel.MyLabelText = $"Clicked {strDemo} time";
    });
}

        网上说了很多方案,什么计算高度、调用 ForceLayout 方法等,都是坑。在百试不灵后,尝试 Home 键退回桌面,再进入 app 发现,ScrollView 居然正常了。也就是说,ScrollView 已经自适应了高度,只是视图没有刷新。于是调用页面级别的 InvalidateMeasure 方法后,成功实现了视图刷新。

         代码修改后如下:

private void OnCounterClicked(object sender, EventArgs e)
{
    // 主线程更新 ui
    MainThread.BeginInvokeOnMainThread(() =>
    {
        // 大文本,显示时会换行,超出原始高度
        strDemo = "Demo demo demo demo demo demo demo demo demo demo demo demo demo
 demo demo demo demo demo demo demo demo demo demo demo demo demo
 demo demo demo demo demo demo demo demo demo .............";

        // 把大文本字符串赋值给绑定对象,呈现在页面
        viewModel.MyLabelText = $"Clicked {strDemo} time";
        // 当前页面 MainPage 的实例对象
        this.InvalidateMeasure();
    });
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值