我开始开发连接到网络项目的移动应用程序,让我们的病人记录他们的出血情况。在网站上,用户可以点击和选择某些地方的少数人的数字。有一些选择的一个看起来像这样

我并不认为使用标准控件(?)来实现这一点很容易,所以我认为使用WebView来承载处理选择的代码是最容易的。
要设置位置,例如当用户想编辑旧出血时,我只需按照Xamarin指南介绍如何从C#调用JS,但是返回值是无效的。我还没有找到从Xamarin.Forms.WebView获取数据的方法。有什么建议吗?
发布于 2016-09-20 04:09:02
这在很大程度上取决于您试图针对的平台和版本。
如果您真的想使用WebView,您可以查看XLabs杂交WebView,它执行类似的操作。
除了XLabs的方法之外,您可以使用UIWebView.EvaluateJavascript()方法从iOS呈现器中轻松地获得一个字符串,然后可以在Xamarin WebView呈现器(链接)中始终覆盖WebChromClient.OnJsAlert,我已经成功地将其用于Android4.1。
类似的内容(如果某些东西不工作或丢失了,请告诉我,我输入了这些代码并删除了额外的代码,这样可能会出现一些小问题).
Xamarin表格代码:
public class SomethingWebView : WebView { }iOS WebView渲染器与UIWebViewDelegate
[assembly: Xamarin.Forms.ExportRenderer(typeof(SomethingWebView), typeof(Your.Droid.Namespace.SomethingWebViewRenderer))]
namespace Your.Droid.Namespace.Renderer {
public class SomethingWebViewRenderer : WebViewRenderer {
protected override void OnElementChanged(VisualElementChangedEventArgs e) {
base.OnElementChanged(e);
if(e.OldElement == null) {
Delegate = new SomethingWebViewDelegate();
}
}
}
internal class SomethingWebViewDelegate : UIWebViewDelegate {
public override void LoadingFinished(UIWebView webView) {
string something = webView.EvaluateJavascript("(function() { return 'something'; })()");
}
}
}带有WebViewClient和WebChromeClient的Android WebViewClient渲染器
[assembly: Xamarin.Forms.ExportRenderer(typeof(SomethingWebView), typeof(Your.iOS.Namespace.SomethingWebViewRenderer))]
namespace Your.iOS.Namespace.Renderer {
public class SomethingWebViewRenderer : WebViewRenderer {
protected override void OnElementChanged(ElementChangedEventArgs<WebView> e) {
base.OnElementChanged(e);
if(e.OldElement == null) {
Control.Settings.JavaScriptEnabled = true;
Control.SetWebViewClient(new SomethingWebViewClient());
Control.SetWebChromeClient(new SomethingWebChromeClient());
}
}
}
internal class SomethingWebViewClient : WebViewClient {
public override void OnPageFinished(Android.Webkit.WebView view, string url) {
base.OnPageFinished(view, url);
view.LoadUrl("javascript:{(function() { window.alert('something'); })()};");
}
}
public class SomethingWebChromeClient : WebChromeClient {
public override bool OnJsAlert(Android.Webkit.WebView view, string url, string message, JsResult result) {
if(message.StartsWith("something") { //This is where you would look for your magic string, anything starting with your magic string is what you would extract and/or act on
//Do something....
result.Cancel(); //This cancels the JS alert (there are other talked about methods of doing this but this works for me)
return true; //This tells the WebView "we got this"
}
return base.OnJsAlert(view, url, message, result); //Let the WebView handle this since we did not find out special string
}
}
}最后,我看了另一个使用JavaScript将页面重定向到新URL的方法,然后在告诉WebView是否应该进行重定向的情况下,提取WebView当前的URL,如果您找到您的魔术字符串,就会做一些事情。如果没有URL只能像255个字符那么长,那么它就不能满足我的需要,这会很好。
https://stackoverflow.com/questions/39584576
复制相似问题