Xamarin Forms Webview自定义渲染器缩放不起作用

时间:2019-06-04 08:39:57

标签: xamarin webview zoom renderer

好,所以我一直在尝试使自定义渲染器正常工作,但是我遇到了问题。 Xamarin论坛的讨论在这里:https://forums.xamarin.com/discussion/comment/376813#Comment_376813

问题是,当我缩小Webview时,它停留在屏幕的一部分而不适合整个屏幕。

Image

我添加了项目代码:

MyWebView.cs

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

namespace CofarLE_Ejemplo_5
{
    public class MyWebView : WebView
    {
        public int ZoomInLevel
        {
            get { return (int)GetValue(ZoomInLevelProperty); }
            set { SetValue(ZoomInLevelProperty, value); }
        }
        public bool EnableZoomControl
        {
            get { return (bool)GetValue(EnableZoomControlProperty); }
            set { SetValue(EnableZoomControlProperty, value); }
        }
        public static readonly BindableProperty ZoomInLevelProperty = BindableProperty.Create(propertyName: "ZoomInLevel", returnType: typeof(int), declaringType: typeof(MyWebView), defaultValue: 100, propertyChanged: OnZoomInLevelPropertyChanged);
        public static readonly BindableProperty EnableZoomControlProperty = BindableProperty.Create(propertyName: "EnableZoomControl", returnType: typeof(bool), declaringType: typeof(MyWebView), defaultValue: false, propertyChanged: OnEnableZoomChanged);

        private static void OnZoomInLevelPropertyChanged(BindableObject bindable, object oldValue, object newValue)
        {
            var control1 = (MyWebView)bindable;
            control1.ZoomInLevel = (int)newValue;
        }
        private static void OnEnableZoomChanged(BindableObject bindable, object oldValue, object newValue)
        {
            var control1 = (MyWebView)bindable;
            control1.EnableZoomControl = (bool)newValue;
        }


    }
}

MyWebViewRendereriOS.cs

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

[assembly: ExportRenderer(typeof(MyWebView), typeof(MyWebViewRendereriOS))]
namespace CofarLE_Ejemplo_5.iOS
{
    public class MyDelegate : UIScrollViewDelegate
    {
        public UIView myView;
        public UIView ViewForZoom;

        public MyDelegate(UIView view)
        {
            myView = view;
            ViewForZoom = view;
        }

        public override UIView ViewForZoomingInScrollView(UIScrollView scrollView)
        {
            MessagingCenter.Subscribe<object, bool>(this, "zoom", (sender, arg) => {
                if (arg == true)
                {
                    myView.ContentMode = UIViewContentMode.ScaleToFill;

                    ViewForZoom = myView;
                }
                else
                {
                    ViewForZoom = null;
                }
            });

            return ViewForZoom;
        }
    }
    public class MyWebViewRendereriOS : WebViewRenderer
    {
        protected override void OnElementChanged(VisualElementChangedEventArgs e)
        {
            base.OnElementChanged(e);

            if (NativeView != null && e.NewElement != null)
            {
                var control1 = NativeView as UIWebView;


                if (e.OldElement != null)
                {
                    e.OldElement.PropertyChanged -= OnElementPropertyChanged;
                }
                if (e.NewElement != null)
                {
                    e.NewElement.PropertyChanged += OnElementPropertyChanged;
                }

                control1.ScalesPageToFit = true;
                control1.ScrollView.Delegate = new MyDelegate(control1);
                control1.ContentMode = UIViewContentMode.ScaleToFill;
                control1.ScrollView.SizeToFit();

            }
        }

        private void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            var control1 = NativeView as UIWebView;
            if (control1 == null)
            {
                return;
            }

            control1.ScalesPageToFit = true;
            ScrollView.MaximumZoomScale = 2;
            ScrollView.MinimumZoomScale = nfloat.Parse("0.5");
            control1.ScrollView.MaximumZoomScale = 2;
            control1.ScrollView.SizeToFit();
            control1.ScrollView.MinimumZoomScale = nfloat.Parse("0.5");
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您可以尝试一下,这是否是您需要的效果(适用于Android)

1。创建自定义视图 ZoomWebView

public class ZoomWebView : View
{
    public static readonly BindableProperty UriProperty = BindableProperty.Create(
        propertyName: "Uri",
        returnType: typeof(string),
        declaringType: typeof(ZoomWebView),
        defaultValue: default(string));

    public string Uri {
        get { return (string)GetValue (UriProperty); }
        set { SetValue (UriProperty, value); }
    }
}

2。自定义渲染器 ZoomWebViewRenderer

 public class ZoomWebViewRenderer : ViewRenderer<ZoomWebView, Android.Webkit.WebView>
 {
    Context _context;
    public ZoomWebViewRenderer(Context context) : base(context)
    {
        _context = context;
    }

    protected override void OnElementChanged(ElementChangedEventArgs<ZoomWebView> e)
    {
        base.OnElementChanged(e);

        if (Control == null)
        {
            var webView = new Android.Webkit.WebView(_context);
            webView.Settings.UseWideViewPort = true;
            webView.Settings.LoadWithOverviewMode = true;
            webView.Settings.SetSupportZoom(true);
            webView.Settings.BuiltInZoomControls = true;
            webView.Settings.DisplayZoomControls = false;
            webView.SetWebViewClient(new ZoomWebViewClient());
            SetNativeControl(webView);
            Control.LoadUrl(Element.Uri);
        }
    }

    class ZoomWebViewClient : WebViewClient
    {
        public override bool ShouldOverrideUrlLoading(WebView view, IWebResourceRequest request)
        {
            view.LoadUrl(request.Url.ToString());
            return true;
        }
    }
}

3。在xaml页面中调用:

<ContentPage.Content>
    <local:ZoomWebView Uri="https://www.microsoft.com" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" />
</ContentPage.Content>