通过Xamarin.Forms中的WebView中的Javascript onClick事件调用C#函数

Shr*_*i G 3 c# webview hybrid-mobile-app xamarin.forms

我有一个帖子类型WebView,我设法将它与服务响应绑定为字符串,但是我有一些链接,如具有其ID的相关帖子。在单击这些链接时,我希望用户转到该文章。我已经尝试了许多解决方案,但是看起来JavaScript不会在点击时调用,而是在加载时调用,因为我完整的WebView被视为字符串,并且如果将其串联起来,它肯定不会保留为脚本。

这是我完整的WebView代码,所附的屏幕截图是WebView中的链接。

我设法通过将整个响应串联在字符串中来使其工作。以下是帮助我实现这一目标的代码

String getHtml()
{
    var bodyStyle = "<!DOCTYPE html><html><head><style>body{height: 100%;}p{text-align:left;color:#191919;}filter{background-color:#191919;}a:link"
        + "{color:#2588B0; background-color:transparent}a:visited {color:#0099CC; background-color:transparent}"
        + "a:hover{color:#0099CC; background-color:transparent}a:ative{color:#0099CC; background-color:transparent}span{background-color: yellow;color: black}customRight{float: right}</style></head><body>";}

    var refs = bodyStyle;

    refs = refs + "<center><h4>" + response.Title + "<h4></center>";

    if (response.Pc.Count > 0)
    {
        refs = refs + "<center><u>" + Pc +
            "</a></u></center><br>";
    }

    if (string.IsNullOrWhiteSpace(response.Cn))
    {
        refs = refs + "<center>" + response.Cn + "</center><br>";

    }

    if (response.Judges.Count() > 0)
    {
        refs = refs + "<center> <strong>Coram:</strong> " + JudgesN + "</center><br>";
    }

    if (string.IsNullOrWhiteSpace(response.JGD.ToString()))
    {
        refs = refs + "<center> <strong>Decided On:</strong> " + response.JGD + "</center><br>";
    }

    if(string.IsNullOrWhiteSpace(response.AppealType) )
    {
        refs = refs + "<center> <strong>Appeal Type: </strong>" + response.AppealType + "</center><br>";
    }

    if (response.Appellants != null)
    {
        refs = refs + "<left><b>" + apeal + "</b></left>";
        refs = refs + "<customRight>apeal</customRight><br>";

    }
    refs = refs + "<center>VERSUS</center>";

    if (response.Respondants != null)
    {
        refs = refs + "<left><b>" + resp + "</b></left>";
        refs = refs + "<customRight>resps</customRight><br><br>";
    }

    if (string.IsNullOrWhiteSpace(response.FinalVr))
    {
        refs = refs + "<center> <strong>Final:</strong> " + response.FinalVr + "</center><br>";
    }

    if (string.IsNullOrWhiteSpace(response.note))
    {
        refs = refs + "<p> <strong>Note:</strong><br/> " + response.Headnote.ToLowerInvariant() + "</p><br>";
    }

    if(response.Refs != null)
    {
        refs = refs + "<left><b>Refered Jdgmts: </b></left><br>";
        foreach(var obj in response.Refs) 
            {
            if(obj.Jid == null) {
                refs = refs + "<p style='font-size:13px;'>"
                    + obj.Title
                    +"</p>";
            }
            else
            {
                refs = refs + "<p style='font-size:13px;'>" + "<a href=\""
                    + obj.Jid
                         + "\" target=\"_blank\" onClick=\"(function(e){alert('e is here'); "+ loadJt(obj.Jid);+"return false;})(); return false;\">"
                         + obj.Title
                    + "</a>"
                    + "</p>";
            }

    jdgmt = jdgmt.Replace("^^^", "<br/><br/>");
    jdgmt = jdgmt.Replace("<SPARA>", "<p>");
    jdgmt = jdgmt.Replace("</SPARA>", "</p>");

    refs = refs + jdgmt;
    refs = refs + "</body></html>";

    return refs;
}

data.Source = new HtmlWebViewSource { Html = getHtml(), BaseUrl="http://url.com/", BindingContext = response };
Run Code Online (Sandbox Code Playgroud)

这是我的LoadJt函数

string loadJt(string jjid)
{
    loadJmt(jid);// invokes the constructor and updates my VM too.
    return jid;
}
Run Code Online (Sandbox Code Playgroud)

下面是链接的显示方式

随附的屏幕截图可进一步清除内容。

And*_*tão 5

不幸的是,当我们谈论移动版WebView时。通过javascript调用ac#函数,这是一种不好的方法。由于使用webview,这样做非常有限,并且每个平台上也可能会有很多问题

所以我们在这里找到了一种对我来说很好的解决方案,我经常使用它

1)您无需仅在xamarin.forms和HTML上实现CustomRenderer

2)该解决方案适用于呼叫摄像机,以本机方式下载文件以及您想要的其他任何东西,只是拦截导航事件

3)在标记中的HTML实现上

 "<a href=></a>" 
Run Code Online (Sandbox Code Playgroud)

像这样的东西:

"<a href='..MyOnclickMethod?objId='"+obj.Jid+">"
+ obj.Title+ "</a>"
Run Code Online (Sandbox Code Playgroud)

4)在您的Xamarin WebView上

...
webview.Navigating+=WebView_Navigating
...


private void WebView_Navigating(object sender, WebNavigatingEventArgs e)
{
      string url = evt.Url;

      if (url.ToLower().Contains("..MyOnclickMethod"))
      {
         //do your code here...
            evt.Cancel = true;
      }
}
Run Code Online (Sandbox Code Playgroud)

确保您调用evt.Cancel = true; 为了防止Webview继续进行流程申请

如果ID尚未出现:您安装的字符串可能不正确如果您对此进行确认并且尚无法使用,请尝试传递

"<a href>" 
Run Code Online (Sandbox Code Playgroud)

具有您ID的有效网址,例如

"<a href='www.google.com/?objId='"+obj.Jid+">"
Run Code Online (Sandbox Code Playgroud)

希望对你有帮助