通知 webView 中按钮的单击操作 [英] Notify click action on button in webView
问题描述
Windows 商店应用程序 - 我的 xaml 页面中有 webView.这个 webview 在一些 http 地址上显示页面.在加载的页面中是简单的登录公式,当用户按下我的应用程序中的登录按钮时,我需要通知.有什么选择吗?
Windows store App - hi I have webView in my xaml page. This webview show page on some http adress. In loaded page is easy formular for login and I need when user press login button notify in my app. Is there any options?
html部分:
我找到了这样的东西:webView1.Document.GetElementById("").InvokeMember("click");
但它不在 RT/W8 中
I find something like this : webView1.Document.GetElementById("").InvokeMember("click");
but its not in RT/W8
推荐答案
以下是模拟登录按钮上的单击操作的方法:
Here is how you could simulate the click action on your login button:
webView.InvokeScript("eval", new[] { "document.getElementById(\"login\").click();" });
如果您想在单击按钮时收到通知,这里是代码:注册 LoadComplete 并使用以下代码添加点击处理程序:
Here is the code if you want to get notified when the button is clicked: Register for LoadComplete and use the folowing code to add a click handler:
private void WebView_OnLoadCompleted(object sender, NavigationEventArgs e)
{
webView.InvokeScript("eval", new[]
{
"var loginButon=document.getElementById(\"login\");" +
"if (loginButon.addEventListener) {" +
"loginButon.addEventListener(\"click\", clickFunction, false);" +
"} else {" +
"loginButon.attachEvent('onclick', clickFunction);" +
"} " +
"function clickFunction(){" +
" window.external.notify('loginClick');"+
"}"
});
}
在页面构造函数中启用 web 视图通知,您应该指定您正在处理的页面的 uri 或者您可以启用所有 uri:
In the page constructor enable web view notification, you should specify the uri of the page you are working on or you can enable all uri:
webView.AllowedScriptNotifyUris = WebView.AnyScriptNotifyUri;
最后注册 ScriptNotify 事件并使用以下处理程序:
Finally register for the ScriptNotify event and use the following handler :
private void WebView_OnScriptNotify(object sender, NotifyEventArgs e)
{
if (e.Value == "loginClick")
{
//Login button have been clicked
}
}
这篇关于通知 webView 中按钮的单击操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!