我如何处理确认对话框在webview?UWP windows 10应用程序c#
How can i handle confirm dialog in webview? UWP windows 10 app C#
我正在用c#开发uwp应用程序(win10)
我想把我的网站在xaml webview元素。
大多数函数都是可行的。
但是我不能处理确认对话框
例如
这是HTML示例&js代码
<button id="btnConfirm" onclick="confirmBox('sure to delete?')">click me to confirm</button>
<button id="btnAlert" onclick="alert('alert message')">click me to alert</button>
<script type="text/javascript">
function confirmBox(message) {
if (confirm(message)) {
alert("yes");
} else {
alert("no");
}
}
</script>
这是我的xaml代码
<WebView Grid.Row="1" x:Name="webView1" Source="ms-appx-web:///HTMLPage1.html" Width="auto" Height="auto"/>
这是我的c#代码
webView1.ScriptNotify += webView1_ScriptNotify;
webView1.NavigationCompleted += webView1_NavigationCompleted;
async void webView1_NavigationCompleted(WebView sender, WebViewNavigationCompletedEventArgs args)
{
await webView1.InvokeScriptAsync("eval", new string[] { "window.confirm = function(confirmMessage) { window.external.notify('typeConfirm:' + confirmMessage) }" });
await webView1.InvokeScriptAsync("eval", new string[] { "window.alert = function(AlertMessage) { window.external.notify('typeAlert:' + AlertMessage) }" });
}
private async void webView1_ScriptNotify(object sender, NotifyEventArgs e)
{
Windows.UI.Popups.MessageDialog dialog;
string[] messageArray = e.Value.Split(':');
string message;
string type;
if (messageArray.Length > 1)
{
message = messageArray[1];
type = messageArray[0];
}
else
{
message = e.Value;
type = "typeAlert";
}
dialog = new Windows.UI.Popups.MessageDialog(message);
Debug.WriteLine("type=" + type + " ,message=" + message);
if (type.Equals("typeConfirm"))
{
dialog.Commands.Add(new UICommand(
"Yes",
new UICommandInvokedHandler(this.CommandInvokedHandler)));
dialog.Commands.Add(new UICommand(
"Cancel",
new UICommandInvokedHandler(this.CommandInvokedHandler)));
dialog.DefaultCommandIndex = 0;
dialog.CancelCommandIndex = 1;
}
var result = await dialog.ShowAsync();
if (result.Label.Equals("Yes"))
{
// return true;
}
else
{
// return false
}
}
问题是确认js函数总是返回false
在用户点击是或否之前。
我可以得到用户选择的按钮。但是已经太晚了。
js函数"confirm"在这种情况下永远不会返回true。
有人能帮我吗?谢谢。
这行不通。JS在单线程上运行,不会等待c#调用的结果。因此,警报将不会等待来自MessageDialog的结果。
请注意,webview在winrt项目中更适合显示一些静态html。如果你确实想弹出一个MessageDialog作为确认对话框,那么你需要在c#代码中完成之前在javascript中完成的所有其余工作。
例如,如果你想改变html控件的一些文本,你需要准备一个完整的html字符串,然后检查CommandInvokedHandler回调中的命令状态,并让webview导航到(webview . navigatetostring)新的html字符串
如果你需要在应用代码中接收来自webview托管脚本的通知和数据,你需要处理ScriptNotify事件。您可以参考这个示例(场景6)。
相关文章:
- 如何在webView,Android中从@JavascriptInterface方法调用Javascript
- window.onload没有'无法在Android WebView中工作
- Android Webview通过Javascript注入CSS
- Android在webview中加载带有javascript的html文件,得到Uncaught ReferenceEr
- 在Android WebView中通过javascript检测点击HTML选项标记
- 用JavaScript替换Android WebView模板上的html文本
- Hammer.js阻止在Android Webview中点击超链接
- 在android中显示固定高度的webview内的大型内容
- Javascript inject CSS into Swift WebView
- 在webView上加载本地存储的文件时延迟
- WebView返回错误的window.innerHeight
- 如何使用webview在React Native中使用Highcharts
- 在Android WebView中运行Javascript-onPageFinished循环
- Chrome 50的Android WebView在触摸页面之前会阻止Javascript
- 在Android的Webview中添加Javascript
- HTML5画布支持和Android Webview
- UWP webView相当于Android中的addJavascriptInterface()
- 我如何处理确认对话框在webview?UWP windows 10应用程序c#
- 如何在Windows 10 UWP中调整基于HTML内容的Webview高度
- 如何在WebView Windows 10 UWP中调用javascript