如何在 Xamarin Forms 中缩放 WebView?

问题描述 投票:0回答:2

我无法缩放 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>

This is my WebView

xamarin xamarin.forms webview
2个回答
4
投票

针对 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);

1
投票

我在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; 
           } 
       } 
   } 
}
© www.soinside.com 2019 - 2024. All rights reserved.