我无法使用 CefSharp 从 WinForms 的 c# 获取一些数组
I can't get some array from c# at WinForms with CefSharp
我有使用 Chromium 的 Win Forms 应用程序。我的目标是获取字符串变量 (json), 在 C# 领域生成,用于 javascript 函数。我在 javascript-land 买不到。
我在 JSObj.cs 创建了 mefod getJSON() - 它生成了 json。我从方法中看到了字符串。我在 Form1.cs 注册了 oblect JSObj(这里我有 Chromium)。我用按钮从 html 调用了 JSObj.getJSON(),但我没有在 javascript 代码中调用 json!
<button class="btn btn-info" id="btn3">Test Winform Interaction</button>
$("#btn3").on('click', function () {
alert(jSObject.getJSON()[0]);
});
public string getJSON()
{
DispHandler hand = new DispHandler(delegate
{
string directoryPath = @"C:\";
List<SObject> sendObjects = new List<SObject>();
DirectoryInfo directoryInfo = new DirectoryInfo(directoryPath);
FileInfo[] filesArray = directoryInfo.GetFiles();
foreach (var el in filesArray)
{
sendObjects.Add(new SObject(el.Name, directoryPath));
}
string json = JsonConvert.SerializeObject(sendObjects);
return json;
});
IAsyncResult resultObj = hand.BeginInvoke(null, null);
var res = hand.EndInvoke(resultObj);
return res;
}
public Form1()
{
InitializeComponent();
InitializeChromium();
_browser.RegisterAsyncJsObject("jSObject", new JSObj());
}
没有错误。我希望在 javascript-land 中获得 json 数据。
C# 和 CEF 之间的通信是异步的,jSObject.getJSON() returns 对结果的承诺,而不是结果本身。试试这个 JS 代码:
$("#btn3").on('click', function () {
jSObject.getJSON().then(function (r) { alert(r[0]); });
});
我有使用 Chromium 的 Win Forms 应用程序。我的目标是获取字符串变量 (json), 在 C# 领域生成,用于 javascript 函数。我在 javascript-land 买不到。
我在 JSObj.cs 创建了 mefod getJSON() - 它生成了 json。我从方法中看到了字符串。我在 Form1.cs 注册了 oblect JSObj(这里我有 Chromium)。我用按钮从 html 调用了 JSObj.getJSON(),但我没有在 javascript 代码中调用 json!
<button class="btn btn-info" id="btn3">Test Winform Interaction</button>
$("#btn3").on('click', function () {
alert(jSObject.getJSON()[0]);
});
public string getJSON()
{
DispHandler hand = new DispHandler(delegate
{
string directoryPath = @"C:\";
List<SObject> sendObjects = new List<SObject>();
DirectoryInfo directoryInfo = new DirectoryInfo(directoryPath);
FileInfo[] filesArray = directoryInfo.GetFiles();
foreach (var el in filesArray)
{
sendObjects.Add(new SObject(el.Name, directoryPath));
}
string json = JsonConvert.SerializeObject(sendObjects);
return json;
});
IAsyncResult resultObj = hand.BeginInvoke(null, null);
var res = hand.EndInvoke(resultObj);
return res;
}
public Form1()
{
InitializeComponent();
InitializeChromium();
_browser.RegisterAsyncJsObject("jSObject", new JSObj());
}
没有错误。我希望在 javascript-land 中获得 json 数据。
C# 和 CEF 之间的通信是异步的,jSObject.getJSON() returns 对结果的承诺,而不是结果本身。试试这个 JS 代码:
$("#btn3").on('click', function () {
jSObject.getJSON().then(function (r) { alert(r[0]); });
});