通过Xamarin.Forms中的WebView中的Javascript onClick事件调用C#函数 [英] Calling a C# function through Javascript onClick event in WebView in Xamarin.Forms
问题描述
我有一个帖子类型WebView
,我设法将它作为字符串与服务响应绑定,但是我有一些链接,例如具有其ID的相关帖子.在单击那些链接时,我希望用户转到该文章.我尝试了许多解决方案,但是 JavaScript 看起来并没有点击,而是在加载时调用,因为我完整的 WebView 被视为字符串,如果我将其串联起来,它绝对不是脚本.
I have a post type WebView
which I managed to bind with the service response as string but I have some links like related posts which have their ids. On clicking those links I want the user to go to that article. I have tried many solutions but its look like the JavaScript doesn't calls on click, it calls on load because my complete WebView is treated as string and if I concatenate it, it definitely doesn't remains a script.
这是我完整的WebView代码,所附的屏幕快照是在WebView中创建的链接.
Here is my complete WebView code and the screenshot attached is the link which is made in WebView.
我设法通过将整个响应串联在字符串中来使其工作. 以下是帮助我实现这一目标的代码
I managed to make it work by concatenating the whole response in string. Below is the code which helped me achieve this
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 };
这是我的LoadJt函数
Here is my LoadJt Function
string loadJt(string jjid)
{
loadJmt(jid);// invokes the constructor and updates my VM too.
return jid;
}
下面的链接显示方式
推荐答案
不幸的是,当我们在移动设备上谈论WebView时.通过javascript调用c#函数是一种不好的方法. 由于使用webview,这样做非常有限,并且每个平台上也可能会遇到很多问题
Unfortunately when we talk about WebView on mobile. Call a c# function through javascript it's a bad way to follow. Because of webview, it's very limited to do that and a lot of problems that you probably will have on each platform too
所以我们在这里找到一种对我来说很好的解决方案,我经常使用它
So here we go a solution that works fine for me and I used it a lot
1)您无需仅在xamarin.forms和HTML上实现CustomRenderer.
1) You won't need to implement a CustomRenderer, just on your xamarin.forms and HTML
2)该解决方案适用于呼叫摄像机,以本机方式下载文件以及您想要的其他任何东西,只需拦截导航事件即可
2) This solution works to Call camera, download files in a native way, and anything else you want, just intercept de navigating event
3)在标记中的HTML实现上
3) On your HTML implemet in your tag
"<a href=></a>"
类似这样的东西:
"<a href='..MyOnclickMethod?objId='"+obj.Jid+">"
+ obj.Title+ "</a>"
4)在您的Xamarin WebView上
4) On your 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;
}
}
确保您调用 evt.Cancel = true; 为防止Webview继续进行流程申请
make sure that you calling evt.Cancel = true; To prevent the webview continuing process requisition
如果ID尚未出现: 您安装的字符串可能不正确 如果您确定此方法仍无法解决问题,请尝试通过
If the id not coming yet: The string that you mounted maybe is not correct If you sure about this and it not working yet, try the pass in
"<a href>"
一个有效的URL,其ID如
a valid URL with your id like
"<a href='www.google.com/?objId='"+obj.Jid+">"
希望我能对您有所帮助
这篇关于通过Xamarin.Forms中的WebView中的Javascript onClick事件调用C#函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!