我目前正在尝试创建一个自定义 ScrollViewer,它具有缩放和pan滚动功能,实现 UIKit 的 ScrollView,但有一个错误,每当屏幕缩放并且应用程序从睡眠状态恢复时,视图都会更改其位置,缩小其位置ScrollHeight 和缩小将网格定位在屏幕的左上角。 (请参阅下面的屏幕截图。)
这可能是 .NET Maui 本身的一个错误,但我不确定。 (在 Xamarin.Forms 上,从睡眠状态恢复后,网格会自动缩小,而不更改视图的位置。)
有解决方法或解决方法吗?
更新:在缩放状态下按下网格内的按钮也会产生相同的错误。
下面是重现问题的代码:
主页.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 .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();
}
}
}
我希望 ScrollViewer 能够正常工作,就像它在 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(尚未在较低版本上测试)
不幸的是,我无法找到解决方法来使 Zoom 功能在 ScrollViewHandler 上正常工作。 相反,我重用了 XF 的 ScrollViewRenderer,并手动设置 ViewForZooming 的 Frame 作为解决方法。 这消除了屏幕在睡眠或点击按钮后重新定位到屏幕之外的错误。
var frameSize = new CGSize(this.Width, this.Height);
var nativeView = ((UIKit.UIView)zoomableSV.Handler.PlatformView);
nativeView.Subviews[0].Frame = new CGRect(x: 0, y: 0, width: frameSize.Width , height: frameSize.Height);