通过 Xamarin.Forms 中 WebView 中的 Javascript onClick 事件调用 C# 函数 [英] Calling a C# function through Javascript onClick event in WebView in 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 + ""+ response.Title + "<h4></center>";如果(响应.Pc.Count > 0){refs = refs + ""+ 电脑 +"</a></u></center><br>";}if (string.IsNullOrWhiteSpace(response.Cn)){refs = refs + "<中心>"+ response.Cn + "</center><br>";}如果 (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>申诉类型:</strong>"+ response.AppealType + "</center><br>";}如果(响应.上诉人!= 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>"+ 响应 + "</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>注意:</strong><br/>" + response.Headnote.ToLowerInvariant() + "</p><br>";}如果(响应.参考!= 空){refs = refs + "<left><b>Refred Jdgmts: </b></left><br>";foreach(var obj in response.Refs){if(obj.Jid == null) {refs = refs + "<p style='font-size:13px;'>"+ 对象.标题+"</p>";}别的{refs = refs + "<p style='font-size:13px;'>"+ " 解决方案 不幸的是,当我们谈论移动设备上的 WebView 时.通过 JavaScript 调用 C# 函数是一种糟糕的方式.由于 webview,它的实现非常有限,并且您可能在每个平台上也会遇到很多问题
所以这里有一个对我来说很好用的解决方案,我经常使用它
1) 您不需要实现 CustomRenderer,只需在您的 xamarin.forms 和 HTML 上
2)此解决方案适用于调用相机,以本机方式下载文件,以及您想要的任何其他内容,只需拦截导航事件
3) 在标签中的 HTML 工具上
"<a href=></a>>
像这样:
"<a href='..MyOnclickMethod?objId="+obj.Jid+"'>"+ obj.Title+</a>"
4) 在您的 Xamarin WebView 上
<代码>...webview.Navigating+=WebView_Navigating...私有无效WebView_Navigating(对象发送者,WebNavigatingEventArgs evt){字符串 url = evt.Url;if (url.Contains(..MyOnclickMethod")){//在这里做你的代码...evt.Cancel = true;}}
确保您调用 evt.Cancel = true;防止webview继续进程请求
如果 id 还没有出现:您安装的字符串可能不正确如果您确定这一点并且它尚未工作,请尝试传入
"
带有您的 id 的有效 URL
"<a href='www.google.com/?objId='"+obj.Jid+">"
希望对你有帮助
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.
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 };
Here is my LoadJt Function
string loadJt(string jjid)
{
loadJmt(jid);// invokes the constructor and updates my VM too.
return jid;
}
Below how links are displaying
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) You won't need to implement a CustomRenderer, just on your xamarin.forms and HTML
2) This solution works to Call camera, download files in a native way, and anything else you want, just intercept the navigating event
3) On your HTML implement in your tag
"<a href=></a>"
something like this:
"<a href='..MyOnclickMethod?objId="+obj.Jid+"'>"
+ obj.Title+ "</a>"
4) On your Xamarin WebView
...
webview.Navigating+=WebView_Navigating
...
private void WebView_Navigating(object sender, WebNavigatingEventArgs evt)
{
string url = evt.Url;
if (url.Contains("..MyOnclickMethod"))
{
//do your code here...
evt.Cancel = true;
}
}
make sure that you calling evt.Cancel = true; To prevent the webview continuing process requisition
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>"
a valid URL with your id like
"<a href='www.google.com/?objId='"+obj.Jid+">"
I hope I have helped you
这篇关于通过 Xamarin.Forms 中 WebView 中的 Javascript onClick 事件调用 C# 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!