我正在尝试使用RegisterJsObject在两个cefsharp实例之一中加载网站URL。该URL是从网站(chromeBrowser1)收到的,但它不会更新窗口(chromeBrowser2)。我试图尽可能地记录脚本,以便更容易理解。我很确定在C#中具有一般知识的人会发现我的问题,我花了好几个小时试图找到解决方案,但我不能。
private void Form1_Load(object sender, EventArgs e)
{
CefSettings settings = new CefSettings();
Cef.Initialize(settings);
chromeBrowser2 = new ChromiumWebBrowser("http://google.com") // Initializate Browser 2 FIRST ( This is the instance we want to update every X seconds using chromeBrowser2.Load("http://somewebsite.com"); later on )
{
Dock = DockStyle.Fill,
};
splitContainer1.Panel2.Controls.Add(chromeBrowser2);
chromeBrowser1 = new ChromiumWebBrowser("http://127.0.0.1/") // Initializate Browser 1 - Main website - source of data and more
{
Dock = DockStyle.Fill,
};
splitContainer1.Panel1.Controls.Add(chromeBrowser1);
chromeBrowser1.RegisterJsObject("callbackObj", new CallbackObjectForJs()); /// the link is obtained from here - See picture
}
public class CallbackObjectForJs
{
private readonly static BackgroundWorker backgroundWorker1 = new BackgroundWorker();
private readonly static ChromiumWebBrowser chromeBrowser2 = new ChromiumWebBrowser("http://facebook.com");
// IF I REMOVE ANY OF THE ABOVE I WILL RECEIVE THE FOLLOWING ERROR :
// Error CS0120 An object reference is required for the non-static field, method, or property 'Form1.backgroundWorker1'
// Error CS0120 An object reference is required for the non-static field, method, or property 'Form1.chromeBrowser2'
public void showMessage(string msg)
{
var sitelink = msg;
Console.WriteLine(sitelink); // WORKING - link is visible on console but chromeBrowser 2 is not update, still reads http://google.com
Console.WriteLine("Loading site using one of the options below"); // Can be seen on console +
// AT THIS POINT chromeBrowser2 SHOULD LOAD THE NEW URL ///
chromeBrowser2.Load(sitelink); // NOT WORKING
Console.WriteLine("Check Point 1");
backgroundWorker1.RunWorkerAsync(sitelink); // NOT WORKING
Console.WriteLine("Check Point 2");
}
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) // NOT WORKING
{
Console.WriteLine("Background Worker"); // Nothing
string link = (string)e.Argument;
Console.WriteLine("Check Point 3");
chromeBrowser2.Load(link);
Console.WriteLine("Check Point 5");
Console.WriteLine(link);
Console.WriteLine("Check Point 6");
}
据我所知,由于某种原因,我所选择的选项都没有被触发。我有很高的猜测,因为错误CS0120,我设法隐藏,使它们静态,但这就是它。我没有想法。搜索结果让我走了这么远。
答案 0 :(得分:0)
我安装了ReSharper,它在不到一分钟的时间内发现了问题。
我所要做的就是让chromeBrowser2
静止。
public partial class Form1 : Form
{
public ChromiumWebBrowser chromeBrowser1;
public *static* ChromiumWebBrowser chromeBrowser2;
....
感谢您的任何意见。