wpf webbrowser javascript回调

时间:2016-10-10 07:15:14

标签: javascript c# jquery wpf

如果有javascript:

function calculateValues(callback)
        {

            window.external.getHistoryRange(0,1,"",function(res){
                var hist = eval(res);
                histCount = hist.historyCount;
                hist = hist.historyContent;
                if (histCount == 0)
                {
                    return;
                }
                $("#history_table").show();
                var $row = addAlertHistoryRow(hist[0]);
                var rowHeight = $row.height();
                pageItemsCount = Math.floor(contentHeight / rowHeight);
                curPageNum = 0;
                $row.remove();

                if (callback) callback();
            });


        }
函数calculateValues(callback)中的

回调参数是:

function(){statItempos = 0; gethistoryandshow(pageNum,startItemsPos,callback);}

和c#代码,适用于该脚本(ObjectForScripting):

public string getHistoryRange(string strVar0 = "", string strVar1 = "", string strVar2 = "", string strVar3 = "")
        {
            string res = "";
            using (DeskAlertsDbContext db = new DeskAlertsDbContext())
            {

                var alerts = db.HistoryAlerts.OrderBy(a => a.ReciveTime)
                    .Include(b => b.alert.WINDOW)
                    .ToList();
                foreach (var alert in alerts)
                {

                    res += ("{\"id\":" + System.Web.HttpUtility.JavaScriptStringEncode(alert.alert.Alert_id) +
                            ",\"date\":\"" +
                            System.Web.HttpUtility.JavaScriptStringEncode(
                                alert.ReciveTime.ToString(CultureInfo.InvariantCulture)) + "\",\"alert\":\"" +
                            System.Web.HttpUtility.JavaScriptStringEncode(alert.alerttext) + "\",\"title\":\"" +
                            System.Web.HttpUtility.JavaScriptStringEncode(alert.alert.Title) + "\",\"acknow\":\"" +
                            System.Web.HttpUtility.JavaScriptStringEncode(alert.alert.Acknown) + "\",\"create\":\"" +
                            System.Web.HttpUtility.JavaScriptStringEncode(alert.alert.Create_date) + "\",\"class\":\"" +
                            "1" + "\",\"urgent\":\"" + System.Web.HttpUtility.JavaScriptStringEncode(alert.alert.Urgent) +
                            "\",\"unread\":\"" + Convert.ToInt32(alert.isclosed).ToString() + "\",\"position\":\"" +
                            System.Web.HttpUtility.JavaScriptStringEncode(alert.alert.Position) + "\",\"ticker\":\"" +
                            alert.alert.Ticker + "\",\"to_date\":\"" +
                            System.Web.HttpUtility.JavaScriptStringEncode(alert.alert.To_date) + "\"},");
                }

                res = res.TrimEnd(','); //trim right ","
                res = "({\"historyCount\":" + alerts.Count.ToString() + ",\"historyContent\":[" + res + "]});";

                Browserwindow.Wb.InvokeScript("eval", new object[] { strVar3 });
                Browserwindow.Wb.InvokeScript("CallbackFunction", new object[] { res });

                return res;
            }

        }

On string:" Browserwindow.Wb.InvokeScript("eval", new object[] { strVar3 });" 我尝试从javascript调用匿名函数并出错。

问题是:如何制作这种逻辑。如何执行JS函数的参数不同的功能。然后继续JS。如果我试图给函数命名,并调用它,函数有效,但全局上下文(if (callback) callback();)变得不可用

1 个答案:

答案 0 :(得分:0)

嗯...刚刚制作了我的变量动态(不是字符串),而且都工作了

public string getHistoryRange(string strVar0 = "", string strVar1 = "", string strVar2 = "", dynamic strVar3 = null)
        {
            string res = "";
            using (DeskAlertsDbContext db = new DeskAlertsDbContext())
            {

                var alerts = db.HistoryAlerts.OrderBy(a => a.ReciveTime)
                    .Include(b => b.alert.WINDOW)
                    .ToList();
                foreach (var alert in alerts)
                {

                    res += ("{\"id\":" + System.Web.HttpUtility.JavaScriptStringEncode(alert.alert.Alert_id) +
                            ",\"date\":\"" +
                            System.Web.HttpUtility.JavaScriptStringEncode(
                                alert.ReciveTime.ToString(CultureInfo.InvariantCulture)) + "\",\"alert\":\"" +
                            System.Web.HttpUtility.JavaScriptStringEncode(alert.alerttext) + "\",\"title\":\"" +
                            System.Web.HttpUtility.JavaScriptStringEncode(alert.alert.Title) + "\",\"acknow\":\"" +
                            System.Web.HttpUtility.JavaScriptStringEncode(alert.alert.Acknown) + "\",\"create\":\"" +
                            System.Web.HttpUtility.JavaScriptStringEncode(alert.alert.Create_date) + "\",\"class\":\"" +
                            "1" + "\",\"urgent\":\"" + System.Web.HttpUtility.JavaScriptStringEncode(alert.alert.Urgent) +
                            "\",\"unread\":\"" + Convert.ToInt32(alert.isclosed).ToString() + "\",\"position\":\"" +
                            System.Web.HttpUtility.JavaScriptStringEncode(alert.alert.Position) + "\",\"ticker\":\"" +
                            alert.alert.Ticker + "\",\"to_date\":\"" +
                            System.Web.HttpUtility.JavaScriptStringEncode(alert.alert.To_date) + "\"},");
                }

                res = res.TrimEnd(','); //trim right ","
                res = "({\"historyCount\":" + alerts.Count.ToString() + ",\"historyContent\":[" + res + "]});";

                dynamic variable = Browserwindow.Wb.InvokeScript("eval", new object[] { strVar3 });
                variable(res);

                return res;
            }

        }