Android同时使用WebViewClient和WebChromeClient

时间:2014-09-03 09:20:51

标签: android xamarin webviewclient webchromeclient

我需要操纵html中的链接,所以我使用WebViewClient来完成它。问题是我有一些加载本地html页面的iframe并且它们没有显示。 但是,如果我使用WebChromeClient,他们就可以了!

所以,我想使用它们两者但它不起作用。似乎只有webViewClient可以工作,我不知道为什么。

我在清单中设置了Internet权限并写入外部存储(因为我的html位于存储中)并启用了硬件加速。

这是我的代码:

public class BChromeClient : WebChromeClient
    {
        public BChromeClient ()
        {}

        public bool OnShowCustomView(WebView view, string url)
        {
            return true;
        }

        public void OnHideCustomView()
        {
        }


    }

    public class BWebClient : WebViewClient
    {
        int _position;
        string _path;
        Activity _parent;
        ViewPager _pager;
        string _chapName;
        public BWebClient (int position, string Path, Activity Parent, ViewPager Pager, string ChapName){
            _position = position;
            _parent = Parent;
            _path = Path;
            _pager = Pager;
            _chapName = ChapName;
        }

        public override void OnPageFinished (WebView view, string url)
        {
            base.OnPageFinished (view, url);
            view.ScrollTo (0, _position);
        }

        public override bool ShouldOverrideUrlLoading (WebView view, string url)
        {

            if (url.StartsWith ("navigate")) {
                string destination = url.Substring (url.IndexOf ("navigate://") + "navigate://".Length);
                int DestinationChapter = Int32.Parse (destination.Substring (0, destination.IndexOf("_")));
                int l = destination.IndexOf("_") + 1;
                int b = destination.Length - l;
                int DestinationPage = Int32.Parse (destination.Substring (l,b));



                if (DestinationPage == 0) {
                    _pager.SetCurrentItem(DestinationChapter ,true);
                    WebView _web = (WebView)_pager.FindViewWithTag(300 + DestinationChapter);
                    _web.LoadUrl ("file://" + _path + "/" + _chapName);
                }



            } else if (url.StartsWith ("pdf")) {
                string file_path = _path + url.Substring (5);
                if (System.IO.File.Exists(file_path)) {
                    Android.Net.Uri pdfFile = Android.Net.Uri.FromFile (new Java.IO.File (file_path));
                    Intent pdfIntent = new Intent (Intent.ActionView);
                    pdfIntent.SetDataAndType (pdfFile, "application/pdf");
                    pdfIntent.SetFlags (ActivityFlags.NoHistory);
                    _parent.StartActivity (pdfIntent);
                }
            }

            return true;
        }
    }

在片段OnCreateView方法中(是的,我在PageViewer中使用webview,但我不认为知道它很重要。)

web_view = view.FindViewById<WebView> (Resource.Id.webview);
        web_view.SetWebChromeClient (new BChromeClient ());
        web_view.SetWebViewClient(new BWebClient(_position,_path,_parent, _pager, _chap.Name ));
        web_view.SetBackgroundColor(Color.Transparent);
        web_view.Settings.JavaScriptEnabled = true;
        web_view.Settings.AllowFileAccess = true;
        //web_view.Settings.SetPluginState (WebSettings.PluginState.On);

1 个答案:

答案 0 :(得分:0)

在MainActivity.cs

web_view..webviewClient = new ExtendedWebViewClient();
 web_view.SetWebChromeClient(new ExtendedChromeClient(MainActivity));
            web_view.SetWebViewClient(this.webviewClient);