コード例 #1
0
ファイル: WebViewPage.cs プロジェクト: zacker150/Ao3-Tracker
        public Xamarin.Forms.View CreateWebView()
        {
            webView = new MyWebView(MainActivity.Instance, this);
            webView.SetWebViewClient(webClient = new WebClient(this));
            webView.SetWebChromeClient(new ChromeClient(this));
            webView.Settings.AllowContentAccess  = true;
            webView.Settings.JavaScriptEnabled   = true;
            webView.Settings.BuiltInZoomControls = true;
            webView.Settings.DisplayZoomControls = false;
            webView.Settings.UseWideViewPort     = true;
            if (Android.OS.Build.VERSION.SdkInt >= BuildVersionCodes.Kitkat)
            {
#if DEBUG
                WebView.SetWebContentsDebuggingEnabled(true);
#else
                WebView.SetWebContentsDebuggingEnabled(false);
#endif
            }
            if (Android.OS.Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
            {
                webView.Settings.MixedContentMode = MixedContentHandling.AlwaysAllow;
            }
            if (Android.OS.Build.VERSION.SdkInt >= BuildVersionCodes.N)
            {
                webView.Settings.DisabledActionModeMenuItems = MenuItems.ProcessText | MenuItems.Share | MenuItems.WebSearch;
            }

            AddJavascriptObject("Ao3TrackHelperNative", helper);

            var placeholder = new Android.Views.View(Forms.Context);
            contextMenuPlaceholder = placeholder.ToView();
            Xamarin.Forms.AbsoluteLayout.SetLayoutBounds(contextMenuPlaceholder, new Rectangle(0, 0, 0, 0));
            Xamarin.Forms.AbsoluteLayout.SetLayoutFlags(contextMenuPlaceholder, AbsoluteLayoutFlags.None);
            helper = new Ao3TrackHelper(this);

            contextMenu = new ContextMenu(Forms.Context, placeholder);
            var menu = contextMenu.Menu;
            for (int i = 0; i < ContextMenuItems.Count; i++)
            {
                var kvp = ContextMenuItems[i];
                if (kvp.Key == "-")
                {
                    // Looks better without
                    //menu.Add(Menu.None, i, i, "\x23AF\x23AF\x23AF\x23AF").SetEnabled(false);
                }
                else
                {
                    menu.Add(Android.Views.Menu.None, i, i, kvp.Key);
                }
            }

            contextMenu.MenuItemClick += (sender, arg) =>
            {
                ContextMenuItems[arg.Item.ItemId].Value?.Execute(contextMenu.CommandParameter);
            };

            MainContent.Children.Add(contextMenuPlaceholder);

            webView.FocusChange += WebView_FocusChange;

            return(webView.ToView());
        }