Xamarin:如何在[Android] WebView中显示网页的一小部分?

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

我试图在Xamarin Android Webkit WebView中只显示一小部分网页。我已经在android studio中看到了很多答案如何做到这一点,但是我没有看到很多关于如何在xamarin for android中做到这一点的答案。你会为这项任务推荐什么方法?我还可以选择加载WebView,然后向下滚动到页面的某个部分。

这是我用来生成制表符和WebView的代码

//Discover new content
[Activity]
//define class discover content
public class SessionsActivity : Activity
{

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        SetContentView(Resource.Layout.discover);

        //declare webview and tell our code where to find the XAML resource
        WebView discoverWebView = FindViewById<WebView>(Resource.Id.webViewDiscover);

        //set the webview client
        discoverWebView.SetWebViewClient(new WebViewClient());
        //load the subscription url
        discoverWebView.LoadUrl("https://www.bitchute.com/");
        //enable javascript in our webview
        discoverWebView.Settings.JavaScriptEnabled = true;
        //zoom control on?  This should perhaps be disabled for consistency?
        //we will leave it on for now
        discoverWebView.Settings.BuiltInZoomControls = true;
        discoverWebView.Settings.SetSupportZoom(true);
        //scrollbarsdisabled
        // subWebView.ScrollBarStyle = ScrollbarStyles.OutsideOverlay;
        discoverWebView.ScrollbarFadingEnabled = false;


    }
}

提前致谢! =]

c# xamarin xamarin.android android-webview
2个回答
0
投票

如果你看一下WebView的Xamarin文档,你会发现它有一个与Java方法具有相同签名的LoadURL方法。唯一的区别是方法的外壳 - Xamarin通常使用C#外壳,但是方法名称是相同的,你传递的参数也是相同的

discoverWebView.LoadUrl("javascript:(function() { " +
"document.getElementsByTagName('header'[0].style.display="none"; " + 
"})()"); 

0
投票

这是我如何做到的,@ Jason是有帮助的,但更完整的答案如下,这加载duckduckgo.com然后隐藏content_homepage,滚动到mid-bottom:

using Android.Graphics.Drawables;
using Android.OS;
using Android.Support.V4.App;
using Android.Views;
using Android.Webkit;
using Android.Widget;

namespace BottomNavigationViewPager.Fragments
{
    public class TheFragment1 : Fragment
    {
        string _title;
        string _icon;

        protected static WebView _wv;

        public static TheFragment1 NewInstance(string title, string icon) {
            var fragment = new TheFragment1();
            fragment.Arguments = new Bundle();
            fragment.Arguments.PutString("title", title);
            fragment.Arguments.PutString("icon", icon);
            return fragment;
        }

        public override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            if (Arguments != null)
            {
                if(Arguments.ContainsKey("title"))
                    _title = (string)Arguments.Get("title");

                if (Arguments.ContainsKey("icon"))
                    _icon = (string)Arguments.Get("icon");
            }
        }

        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {

            var view = inflater.Inflate(Resource.Layout.TheFragmentLayout1, container, false);

            _wv = view.FindViewById<WebView>(Resource.Id.webView1);

            var _wvc = new MyWebViewClient();




            _wv.SetWebViewClient(_wvc);

            //string _wtf = "header";



            _wv.Settings.JavaScriptEnabled = true;

            _wv.Settings.AllowFileAccess = true;

            _wv.Settings.AllowContentAccess = true;




            //_wvc.OnPageFinished(_wv, _jsHideBannerC);



            _wv.LoadUrl("https://www.duckduckgo.com/");

            return view;
        }

        private class MyWebViewClient : WebViewClient
        {
            public override void OnPageStarted(WebView view, string url, Android.Graphics.Bitmap favicon)
            {
                base.OnPageStarted(view, url, favicon);


            }

            public override void OnPageFinished(WebView view, string url)
            {
                base.OnPageFinished(view, url);


                string _jsHideBanner = "javascript:(function() { " +
                                "document.getElementById('content_homepage').style.display='none'; " + "})()";

                string _jsHideBannerC = "javascript:(function() { " +
                    "document.getElementsByClassName('logo-wrap--home').style.display='none'; " + "})()";

                _wv.LoadUrl(_jsHideBanner);
            }


        }
    }
    }
© www.soinside.com 2019 - 2024. All rights reserved.