|
| 1 | +using Android.App; |
| 2 | +using Android.Content; |
| 3 | +using Android.OS; |
| 4 | +using Android.Runtime; |
| 5 | +using Android.Views; |
| 6 | +using Android.Widget; |
| 7 | +using Sample.Xamarin.Core.CustomControls; |
| 8 | +using Sample.Xamarin.Droid.Renderer; |
| 9 | +using System; |
| 10 | +using System.Collections.Generic; |
| 11 | +using System.ComponentModel; |
| 12 | +using System.Threading.Tasks; |
| 13 | +using Xamarin.Forms; |
| 14 | +using Xamarin.Forms.Platform.Android; |
| 15 | +using static Android.Webkit.WebSettings; |
| 16 | + |
| 17 | +[assembly: ExportRenderer(typeof(CustomWebView), typeof(CustomWebViewRenderer))] |
| 18 | +namespace Sample.Xamarin.Droid.Renderer |
| 19 | +{ |
| 20 | + public class CustomWebViewRenderer : WebViewRenderer |
| 21 | + { |
| 22 | + public CustomWebViewRenderer(Context context) : base(context) |
| 23 | + { |
| 24 | + |
| 25 | + } |
| 26 | + |
| 27 | + protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) |
| 28 | + { |
| 29 | + base.OnElementPropertyChanged(sender, e); |
| 30 | + |
| 31 | + Control.SetWebViewClient(new ExtendedWebViewClient(Element as CustomWebView)); |
| 32 | + |
| 33 | + } |
| 34 | + |
| 35 | + class ExtendedWebViewClient : Android.Webkit.WebViewClient |
| 36 | + { |
| 37 | + private CustomWebView _xwebView { get; set; } |
| 38 | + public ExtendedWebViewClient(CustomWebView webView) |
| 39 | + { |
| 40 | + _xwebView = webView; |
| 41 | + } |
| 42 | + |
| 43 | + async public override void OnPageFinished(Android.Webkit.WebView view, string url) |
| 44 | + { |
| 45 | + if (_xwebView != null) |
| 46 | + { |
| 47 | + _xwebView.HeightRequest = 0d; |
| 48 | + await Task.Delay(100); |
| 49 | + var result = await _xwebView.EvaluateJavaScriptAsync("(function(){return document.body.scrollHeight;})()"); |
| 50 | + Console.WriteLine(result); |
| 51 | + var newResult = result; |
| 52 | + for (int i = 0; i < 1000 && result == newResult; i++) |
| 53 | + { |
| 54 | + await Task.Delay(100); |
| 55 | + newResult = await _xwebView.EvaluateJavaScriptAsync("(function(){return document.body.scrollHeight;})()"); |
| 56 | + Console.WriteLine(newResult); |
| 57 | + } |
| 58 | + _xwebView.HeightRequest = Convert.ToDouble(newResult); |
| 59 | + view.SetInitialScale(40); |
| 60 | + |
| 61 | + } |
| 62 | + |
| 63 | + base.OnPageFinished(view, url); |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | +} |
0 commit comments