You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET MAUI iOS自定义ScrollView缩放后异常问题求助

.NET MAUI 自定义缩放ScrollView休眠恢复/按钮点击后视图异常问题

我基于UIKit的ScrollView实现了带缩放功能的自定义ScrollView,但遇到以下Bug:当屏幕处于缩放状态时,应用从休眠恢复或点击网格内按钮,会出现视图位置改变、滚动高度缩小,且缩放恢复后网格定位到屏幕左上角的问题。

不确定这是否是.NET MAUI本身的Bug,因为在Xamarin.Forms中,应用从休眠恢复后网格会自动缩放恢复且不会改变视图位置。求可行的解决办法或规避方案。


可复现问题的代码

MainPage.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"
             xmlns:controls="clr-namespace:MauiAppTest"
             x:Class="MauiAppTest.MainPage">

    <controls:ZoomableScrollView>
        <Grid Background="pink">
            <VerticalStackLayout
            Padding="30,0"
            Spacing="25">
                <Image
                Source="dotnet_bot.png"
                HeightRequest="185"
                Aspect="AspectFit"
                SemanticProperties.Description="dot net bot in a race car number eight" />

                <Label
                Text="Hello, World!"
                Style="{StaticResource Headline}"
                SemanticProperties.HeadingLevel="Level1" />

                <Label
                Text="Welcome to &#10;.NET Multi-platform App UI"
                Style="{StaticResource SubHeadline}"
                SemanticProperties.HeadingLevel="Level2"
                SemanticProperties.Description="Welcome to dot net Multi platform App U I" />

                <Button
                x:Name="CounterBtn"
                Text="Click me" 
                SemanticProperties.Hint="Counts the number of times you click"
                Clicked="OnCounterClicked"
                HorizontalOptions="Fill" />
            </VerticalStackLayout>
        </Grid>
    </controls:ZoomableScrollView>
</ContentPage>

ZoomableScrollView.cs(自定义ScrollView)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MauiAppTest
{
    public class ZoomableScrollView : ScrollView
    {

    }
}

ZoomableScrollViewHandler.cs

using Microsoft.Maui.Handlers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UIKit;

namespace MauiAppTest.Platforms.iOS
{
    public class ZoomableScrollViewHandler : ScrollViewHandler
    {
        protected override void ConnectHandler(UIScrollView platformView)
        {
            base.ConnectHandler(platformView);
            if(platformView != null)
            {
                platformView.MinimumZoomScale = 1;
                platformView.MaximumZoomScale = 3;
                platformView.ViewForZoomingInScrollView += (UIScrollView sv) =>
                {
                    return platformView.Subviews[0];
                };
            }
        }
        protected override void DisconnectHandler(UIScrollView platformView)
        {
            base.DisconnectHandler(platformView);
        }
    }
}

MauiProgram.cs

using Microsoft.Extensions.Logging;
#if IOS
using  MauiAppTest.Platforms.iOS;
#endif

namespace MauiAppTest
{
    public static class MauiProgram
    {
        public static MauiApp CreateMauiApp()
        {
            var builder = MauiApp.CreateBuilder();
            builder
                .UseMauiApp<App>()
                .ConfigureFonts(fonts =>
                {
                    fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
                    fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
                });
#if IOS
builder.ConfigureMauiHandlers(handlers => 
handlers.AddHandler(typeof(ZoomableScrollView),typeof(ZoomableScrollViewHandler)));
#endif
#if DEBUG
            builder.Logging.AddDebug();
#endif

            return builder.Build();
        }
    }
}

预期与已尝试方案

  • 预期效果:该ScrollView能像Xamarin.Forms中一样正常工作
  • 已尝试方案:
    • 使用ScrollViewRenderer(借助maui.compatibility)
    • 在OnSleep时记录ZoomScale和ContentOffset,在OnResume时重新设置

更新:Xamarin.Forms中正常运行的代码

ZoomableScrollView.cs(自定义ScrollView)

using System;
using System.Collections.Generic;
using System.Text;
using Xamarin.Forms;

namespace TestXamarin
{
    public class ZoomableScrollView : ScrollView
    {
    }
}

ZoomableScrollViewRenderer.cs

using Foundation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TestXamarin;
using TestXamarin.iOS;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

[assembly: ExportRenderer(typeof(ZoomableScrollView), typeof(ZoomableScrollViewRenderer))]
namespace TestXamarin.iOS
{
    public class ZoomableScrollViewRenderer : ScrollViewRenderer
    {
        protected override void OnElementChanged(VisualElementChangedEventArgs e)
        {
            base.OnElementChanged(e);

            var test = this.Element as ZoomableScrollView;
             
            this.MinimumZoomScale = 1f;
            this.MaximumZoomScale = 3f;
            this.ViewForZoomingInScrollView += (UIScrollView sv) => { return this.Subviews[0]; };
        }
    }
}

该代码在Xamarin.Forms 4.8和5.0中均可正常运行(未测试更低版本)


内容的提问来源于stack exchange,提问作者Takashi123

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 18:01:26