How can I execute Javascript callback function from C# host application

后端 未结 2 1874
[愿得一人]
[愿得一人] 2020-12-05 20:47

I\'m creating an application in C# that hosts custom web pages for most of the GUI. As the host, I\'d like to provide a javascript API so that the embedded web pages can ac

2条回答
  •  攒了一身酷
    2020-12-05 21:40

    As @Frogmouth noted here already you can pass callback function name to the LongRunningProcedure:

    function onComplete( result )
    {
        alert( result );
    }
    
    function start()
    {
        window.external.LongRunningProcess( 'data', 'onComplete' );
    }
    

    and when LongRunningProcedure completes use .InvokeScript as the following:

        public void LongRunningProcess(string data, string callbackFunctionName)
        {
            // do work, call the callback
    
            string codeStrig = string.Format("{0}('{1}')", callbackFunctionName, "{{ Your result value here}}");
            webBrowser1.Document.InvokeScript("eval", new [] { codeStrig});  
        }
    

提交回复
热议问题