WPF浏览器javascript回调
本文关键字:回调 javascript 浏览器 WPF | 更新日期: 2023-09-27 18:12:14
如果有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();
)变得不可用
您的回调函数名称不正确。
代替Browserwindow.Wb.InvokeScript("CallbackFunction", new object[] { res });
Browserwindow.Wb.InvokeScript("calculateValues", new object[] { res });
嗯…只是让我的变量是动态的(不是字符串),并且都工作
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;
}
}