更好的方式

前端向Foxtable发送消息,其实更好的方式是直接在前端调用Foxtable的自定义函数,将消息作为参数传递给函数,并接收函数的返回值,这样一行代码就能实现双向通讯。

提示:前端脚本调用Foxtable,会在调用《调用Foxtable对象》这一章做详尽的介绍,目前只需有所了解即可,无需理解。

示例

1、首先增加一个自定义函数Max,代码为:

Dim MaxVal As Double = Args(0)
For
i As Integer = 1 To Args.Length - 1
    MaxVal = Math.Max(MaxVal, Args(i))

Next

Return
MaxVal

2、然后增加一个窗口,插入WebViewer控件,将窗口的AfterLoad事件代码设置为:

'''Async
Dim wv As WebViewer = e.Form.Controls("WebViewer1").WebViewer
Await
wv.EnsureCoreWebView2Async(Nothing) ' 初始化WebViewer
Dim
ft As New FoxLib(wv) '创建Foxtable
wv.CoreWebView2.AddHostObjectToScript(
"ft", ft) '注入Foxtable
'
定义测试页面
Dim
html As String = <![CDATA[
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>
自定义函数测试</title>
</head>
<body>
    <h2>
自定义函数测试</h2>
    <button onclick="calcMaxValue()">
执行自定义函数Max</button>
    <script>
        const ft = window.chrome.webview.hostObjects.sync.ft;
        function calcMaxValue() {
           
const v = ft.Functions.Execute("Max", 1, 2, 3, 9);
            ft.MessageBox().Show("
最大值:" + v.toString(), "计算结果", ft.MessageBoxButtons.OK, ft.MessageBoxIcon.Information);
        }
    </script>
</body>
</html>
]]>
.Value
wv.NavigateToString(html)
'
加载测试页面

打开窗口,单击网页中的按钮,即可计算出1、2、3、9中的最大值9。

而数据的传递和接收都在同一行代码:

const v = ft.Functions.Execute("Max", 1, 2, 3, 9);


本页地址:http://www.foxtable.com/webhelp/topics/6374.htm