我无法缩放 Xamarin.Forms 中的 Web 视图。下面是代码,请有人帮助我。谢谢你
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="highchart.MainPage">
<ScrollView>
<WebView VerticalOptions="FillAndExpand" WidthRequest="80" HorizontalOptions="FillAndExpand">
<WebView.Source WebView.EnableZoomControls="true" WebView.ScalesPageToFit = "true">
<HtmlWebViewSource x:Name="HighChart"/>
</WebView.Source>
</WebView>
</ScrollView>
</ContentPage>
针对 Android 特定(在共享项目 [FORMS] 内)
XAML 解决方案:
<ContentPage ...
xmlns:android="clr-namespace:Xamarin.Forms.PlatformConfiguration.AndroidSpecific;assembly=Xamarin.Forms.Core">
<WebView Source="https://www.xamarin.com"
android:WebView.EnableZoomControls="true"
android:WebView.DisplayZoomControls="true" />
C# 解决方案:
using Xamarin.Forms.PlatformConfiguration;
using Xamarin.Forms.PlatformConfiguration.AndroidSpecific;
...
WebView.On<Android>().EnableZoomControls(true);
WebView.On<Android>().DisplayZoomControls(true);
我在iOS上的Android上测试过,看来这个问题只出现在Android上。
您可以创建一个自定义渲染器来实现缩放功能,它在我这边工作得很好。
[assembly: ExportRenderer(typeof(WebView), typeof(CustomWebViewRenderer))]
namespace PDFPOC.Droid
{
public class CustomWebViewRenderer : WebViewRenderer
{
public CustomWebViewRenderer(Context context) : base(context)
{
}
protected override void OnElementChanged(ElementChangedEventArgs<WebView> e)
{
base.OnElementChanged(e);
if (e.NewElement != null)
{
Control.Settings.AllowUniversalAccessFromFileURLs = true;
Control.Settings.SetSupportZoom(true);
Control.Settings.BuiltInZoomControls = true;
Control.Settings.DisplayZoomControls = true;
}
}
}
}