如何在WebView Windows 10 UWP中调用javascript? [英] How to invoke javascript in WebView Windows 10 UWP?

查看:114
本文介绍了如何在WebView Windows 10 UWP中调用javascript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在WebView中加载一个javascript来进行一些计算,并在字符串中获取输出。我尝试使用以下代码

I am trying to load a javascript in WebView to do some calculations and get the output in a string. I tried to use following code

string htmlFragment = "<html><head><script type='text/javascript'>" +
                    "function doubleIt(incoming){ " +
                    "  var intIncoming = parseInt(incoming, 10);" +
                    "  var doubled = intIncoming * 2;" +
                    "  document.body.style.fontSize= doubled.toString() + 'px';" +
                    "  return doubled.toString());" +
                    "};" +
                    "</script></head><body>" +
                    "<div id = 'myDiv'>I AM CONTENT</div></body></html>";
            htmlView.NavigateToString(htmlFragment);
            htmlView.LoadCompleted += async(s1,e1) =>
              {
                  string result = await htmlView.InvokeScriptAsync("eval", new string[] { "doubleIt(25)" });
                  Debug.WriteLine(result);
              };

更新
我现在可以轻松加载简单的javascript基于答案中提供的帮助。但是现在我遇到问题,当javascript中有多个函数时,我得到一个例外。我正在尝试以下代码

Update I am able to load simple javascript easily now based on help provided in the answer. But now I am facing issues when there is more than one function in javascript, I am getting an exception. I am trying the following code

string htmlFragment = @"<html><head><script type='text/javascript'>" +
                    "function a(){return 10;};" +
                    "function b(){return 20;};" +
                    "function c(){return 30;};" +
                    "return (a()*b()*c());" +
                    "</script></head><body>" +
                    "<div id = 'myDiv'>I AM CONTENT</div></body></html>";

请建议。

推荐答案

此功能的文档非常糟糕。花了一些时间来弄清楚如何在UWP中调用 Javascript WebView

The documentation for this feature is really poor. It took me some time to figure out how to invoke Javascript in UWP WebView

当你第一次看到函数调用 webView.InvokeScriptAsync(string,string [])你的初步反应是他们希望函数名作为第一个参数然后函数paramaeters作为字符串数组。 (主要是因为MSDN)文件说明这一点)

When you first look at the function call webView.InvokeScriptAsync(string,string[]) your initial reaction is that they want the function name as the first parameter and then the function paramaeters as the string array. (mainly because the MSDN documentation says this)


参数

scriptName

类型:System.String [.NET] | Platform :: String [C ++]

Type: System.String [.NET] | Platform::String [C++]

要调用的脚本函数的名称。

The name of the script function to invoke.

参数

类型:System.String []
[ .NET] | Platform :: Array [C ++]

Type: System.String[] [.NET] | Platform::Array [C++]


的字符串数组打包脚本函数的参数。

A string array that packages arguments to the script function.

但是,这是错误的,会导致数小时的敲击。真的,他们想要的是第一个参数中的eval一词,然后是一个字符串数组的函数,或者你想要评估的命令

HOWEVER, this is wrong and will lead to hours of head banging. REALLY, what they want is the word "eval" in the first parameter and then a string array of functions, and or commands you wish to eval

   var value = await webViewer.InvokeScriptAsync("eval", 
      new string[] 
      { 
        "functionName(functionParams)" 
      });

我已经使用Microsoft API几年了,我确信这不是预期的方式消耗这个功能,有点黑客。不幸的是,如果你想使用JavaScript,这是我知道当前有效的唯一方式。

Having worked with Microsoft APIs for a few years now I am convinced that this is not the intended way of consuming this function and is a bit of a hack. Unfortunately if you want to consume JavaScript this is the only way that I know that works currently.

这篇关于如何在WebView Windows 10 UWP中调用javascript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆