在Xamarin Forms PCL中从Javascript调用C#函数

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

我正在使用Xamarin Forms在c#中开发一个跨平台的应用程序。我是xamarin的新手。我创建了一个便携式跨平台应用程序。其中我试图在webview中打开本地html页面。对于从javascript调用c#函数,我使用XAMARIN FORUM实现了混合Web视图。

<ContentPage.Content>
    <local:HybridWebView x:Name="hybridWebView"  Uri="index.html" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" />
  </ContentPage.Content>

我正在调用index.html中的invokeCSharpAction()

在Droid项目中

 public class HybridWebViewRenderer : ViewRenderer<HybridWebView, Android.Webkit.WebView>
    {
        const string JavaScriptFunction = "function invokeCSharpAction(data){jsBridge.invokeAction(data);}";

        protected override void OnElementChanged(ElementChangedEventArgs<HybridWebView> e)
        {
            base.OnElementChanged(e);
            Android.Webkit.WebView.SetWebContentsDebuggingEnabled(true);

            if (Control == null)
            {
                var webView = new Android.Webkit.WebView(Forms.Context);
                webView.Settings.JavaScriptEnabled = true;
                webView.Settings.DomStorageEnabled = true;
                Android.Webkit.WebView.SetWebContentsDebuggingEnabled(true);

                webView.SetWebChromeClient(new WebChromeClient());
                webView.SetWebViewClient(new WebViewClient());
                webView.AddJavascriptInterface(new JSBridge(this), "jsBridge");
                SetNativeControl(webView);
            }
            if (e.OldElement != null)
            {
                Control.RemoveJavascriptInterface("jsBridge");
                var hybridWebView = e.OldElement as HybridWebView;
                hybridWebView.Cleanup();
            }
            if (e.NewElement != null)
            {
                Control.AddJavascriptInterface(new JSBridge(this), "jsBridge");
                Control.LoadUrl(string.Format("file:///android_asset/Content/{0}", Element.Uri));
                InjectJS(JavaScriptFunction);
            }
        }

        void InjectJS(string script)
        {
            if (Control != null)
            {
                Control.LoadUrl(string.Format("javascript: {0}", script));
            }
        }

    }

JSBridge.cs

public class JSBridge : Java.Lang.Object
    {
        readonly WeakReference<HybridWebViewRenderer> hybridWebViewRenderer;

        public JSBridge(HybridWebViewRenderer hybridRenderer)
        {
            hybridWebViewRenderer = new WeakReference<HybridWebViewRenderer>(hybridRenderer);
        }

        [JavascriptInterface]
        [Export("invokeAction")]
        public void InvokeAction(String data)
        {

            HybridWebViewRenderer hybridRenderer;

            if (hybridWebViewRenderer != null && hybridWebViewRenderer.TryGetTarget(out hybridRenderer))
            {
                hybridRenderer.Element.InvokeAction(data);
            }
        }

        [JavascriptInterface]
        [Export("invokeDownloadAction")]
        public void invokeDownloadAction(String data)
        {

            HybridWebViewRenderer hybridRenderer;

            if (hybridWebViewRenderer != null && hybridWebViewRenderer.TryGetTarget(out hybridRenderer))
            {
              //  hybridRenderer.Element.InvokeAction(data);
                hybridRenderer.Element.InvokeAction(data);
            }
        }
    }

假设我使用index.html中的window.location导航到home.html现在如果我尝试调用相同的invokeCSharpAction(),在输出窗口中VS说

invokeCSharpAction未定义

我该如何处理这种情况。我可以为我的混合webview进行OnNavigated和OnNavigating事件吗?

c# webview xamarin.forms portable-class-library hybrid
1个回答
0
投票

请参阅此URL以获取Android IOS和UWP https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/custom-renderer/hybridwebview

您可以轻松解决问题

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