简体   繁体   English

Xamarin表单放大在Web视图中缩小不能正常工作

[英]Xamarin Forms Zoom in Zoom out in Web view Does not work Smoothly

I an Custom Render Webview In that I load a Pdf. 我是一个自定义渲染Webview,其中加载了一个Pdf。 When that Pdf is trying to zoom in or zoom out. 当该Pdf尝试放大或缩小时。 I can clearly see lacking. 我可以清楚地看到缺乏。 Frist, I thought it's the android problem then I download pdf and try it It's okay it runs smoothly. Frist,我以为是android问题,然后我下载了pdf并尝试了可以​​顺利运行。

Here my code 这是我的代码

In the shared Project 在共享项目中

  public class CustomWebView : WebView
{
    public static readonly BindableProperty UriProperty = BindableProperty.Create(nameof(Uri),typeof(string),typeof(CustomWebView),default(string));

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

In anrioid Custom Render Like this 在无定形的自定义渲染中

  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.BuiltInZoomControls = true;
            Control.Settings.DisplayZoomControls = true;

        }
        this.Control.SetBackgroundColor(Android.Graphics.Color.Transparent);
    }
    protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        if (e.PropertyName != "Uri") return;
        var customWebView = Element as CustomWebView;
        if (customWebView != null)
        {
            Control.LoadUrl(string.Format("file:///android_asset/pdfjs/web/viewer.html?file={0}", string.Format("file:///android_asset/Content/{0}", WebUtility.UrlEncode(customWebView.Uri))));        
        }
    }

}

It's getting the pdf and also it's showing without problem but when zoom in or out clearly can see the latching How to solve this latching problem. 它正在获取pdf,并且也没有问题,但是当放大或缩小时可以清楚地看到闩锁。如何解决此闩锁问题。

The zoom in/out function in Webview is not smooth Webview中的放大/缩小功能不流畅

you can either make a custom renderer or use Xam.Plugin.WebView plugin for javascript injection.below is the code showing an example using a plugin. 您可以制作自定义渲染器,也可以将Xam.Plugin.WebView插件用于javascript注入。下面的代码显示了使用该插件的示例。 you can call action to zoom in and out. 您可以调用动作来放大和缩小。

        var zoomFactor = 0.1;
        var zoomCounter = 1;
        Func<Task> zoomInAction = async () =>
        {
            zoomFactor = zoomFactor + 0.1;
            if (webView != null)
                await webView.InjectJavascriptAsync($"document.body.style.zoom = {zoomCounter + 1 * zoomFactor};");
        };

        Func<Task> zoomOutAction = async () =>
        {
            zoomFactor = zoomFactor - 0.1;
            if (webView != null)
                await webView.InjectJavascriptAsync($"document.body.style.zoom = {zoomCounter + 1 * zoomFactor};");
        };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM