Beispiel #1
0
        /// <summary>
        /// Asynchronously invoke a Javascript method on this display.
        /// </summary>
        /// <param name="sFunction">The name of the global function to call in the JS.</param>
        /// <param name="tArguments">A list of parameters to pass.</param>
        public void AsyncCallGlobalFunction(String sFunction, params JSValue[] tArguments)
        {
            // If we do not have an active web control.
            if (ActiveControl == null)
            {
                Log.Write("Cannot call method on display (" + this.ToString() + ") which does not have a web visual.", Authority.AUTHORITY_LOG_SOURCE, Log.Type.AppError);
                return;
            }

            // FIXME ASAP
            // THIS IS VERY SLOW BECAUSE IT USES SYNC COMMUNICATION TO GET THE OBJECT TO CALL
            // BUT APPARENTLY THIS IS THE ONLY WAY TO DO IT
            // http://forums.awesomium.com/viewtopic.php?f=4&t=1167&p=1470&hilit=CallJavascriptFunction#p1470
            // THE WAY I AM GOING TO SOLVE THIS FOR NOW IS TO CONVERT ALL THE ARGS TO A JS STRING AND EXECUTE IT.

            // FIXME: Make me properly Async.
            // TODO: Test my little converter function.. its probably crap!
            StringBuilder pString = new StringBuilder();

            if (tArguments != null)
            {
                for (int i = 0, n = tArguments.Length; i < n; ++i)
                {
                    pString.Append(ToJSON(tArguments[i]));
                    if (i < (n - 1))
                    {
                        pString.Append(",");
                    }
                }
            }
            var s = pString.ToString();

            // Push it to the dispatcher (we need to be in the calling thread).
            ActiveControl.Dispatcher.BeginInvoke((Action) delegate()
            {
                try
                {
                    if (ActiveControl == null || !ActiveControl.IsProcessCreated)
                    {
                        return;
                    }

                    var sJavascript = @"if ('" + sFunction + @"' in window) {" + sFunction + @"(" + s + @"); }
else {  }"; // Authority.log('" + sFunction + @" function not found.');
                    //Console.WriteLine(sJavascript);
                    ActiveControl.ExecuteJavascript(sJavascript);
                }
                catch (Exception e)
                {
                    // Just drop exceptions.. icky..
                }
            });
        }