从Javascript中的回调函数获取结果

时间:2021-01-10 09:56:16

I'm writing a Javascript function which will be called by a C# application. I can call the function from C#, but have been unable to retrieve the result of the function.

我正在编写一个Javascript函数,它将由C#应用程序调用。我可以从C#调用该函数,但是无法检索函数的结果。

So I have the following structure:

所以我有以下结构:

var B = function() {
    var A = function() {
        var dfd = new $.Deferred();
            // do something and then return the value I need
            return dfd.resolve(x);
        ......
        return dfd.promise();
    }
    $.when(A()).
        then(function(x) {
            // I can get the x I want here.
            alert(x);
            // What to do at this point?
        });
}

Since A() used asynchronous methods, I chose to use jQuery.promise() method to ensure I get the final result of A(). Now I want B() to return the value x to the C# application. Is there any good solution to this?

由于A()使用了异步方法,我选择使用jQuery.promise()方法来确保得到A()的最终结果。现在我希望B()将值x返回给C#应用程序。这有什么好的解决方案吗?

1 个解决方案

#1


0  

I assume that you've called your Javascript function by calling WebView method InvokeScriptAsync with Js function name as a parameter.

我假设您通过使用Js函数名称作为参数调用WebView方法InvokeScriptAsync来调用Javascript函数。

The problem here is that your JavaScript function is an asynchronous since it uses promise - so you cannot simply get its return value on C#. The IAsyncOperation ends as the promise object has been returned, not as it has been resolved.

这里的问题是你的JavaScript函数是异步的,因为它使用了promise - 所以你不能简单地在C#上获得它的返回值。 IAsyncOperation在返回promise对象时结束,而不是已经解析。

If your javascript function was synchronious:

如果你的javascript函数是同步的:

The return value of the InvokeScriptAsync function is the string result of the script invocation - so if you'll wait for it you'll get the result.

InvokeScriptAsync函数的返回值是脚本调用的字符串结果 - 所以如果你等待它,你将得到结果。

MSDN documentation : https://msdn.microsoft.com/en-us/library/windows.ui.xaml.controls.webview.invokescriptasync.aspx?f=255&MSPPError=-2147217396

MSDN文档:https://msdn.microsoft.com/en-us/library/windows.ui.xaml.controls.webview.invokescriptasync.aspx?f = 255&MSPPError = -2147217396

However, you can always invoke webView event by calling windows.external.invoke with string parameter from JavaScript and get it on C# by subscribing to webView ScriptNotify event.

但是,您始终可以通过使用JavaScript中的字符串参数调用windows.external.invoke来调用webView事件,并通过订阅webView ScriptNotify事件在C#上获取它。

#1


0  

I assume that you've called your Javascript function by calling WebView method InvokeScriptAsync with Js function name as a parameter.

我假设您通过使用Js函数名称作为参数调用WebView方法InvokeScriptAsync来调用Javascript函数。

The problem here is that your JavaScript function is an asynchronous since it uses promise - so you cannot simply get its return value on C#. The IAsyncOperation ends as the promise object has been returned, not as it has been resolved.

这里的问题是你的JavaScript函数是异步的,因为它使用了promise - 所以你不能简单地在C#上获得它的返回值。 IAsyncOperation在返回promise对象时结束,而不是已经解析。

If your javascript function was synchronious:

如果你的javascript函数是同步的:

The return value of the InvokeScriptAsync function is the string result of the script invocation - so if you'll wait for it you'll get the result.

InvokeScriptAsync函数的返回值是脚本调用的字符串结果 - 所以如果你等待它,你将得到结果。

MSDN documentation : https://msdn.microsoft.com/en-us/library/windows.ui.xaml.controls.webview.invokescriptasync.aspx?f=255&MSPPError=-2147217396

MSDN文档:https://msdn.microsoft.com/en-us/library/windows.ui.xaml.controls.webview.invokescriptasync.aspx?f = 255&MSPPError = -2147217396

However, you can always invoke webView event by calling windows.external.invoke with string parameter from JavaScript and get it on C# by subscribing to webView ScriptNotify event.

但是,您始终可以通过使用JavaScript中的字符串参数调用windows.external.invoke来调用webView事件,并通过订阅webView ScriptNotify事件在C#上获取它。