我正在开展一个项目。我在C#Windows窗体中使用CEFSharp(Chromium Embedded Framework)。我的解决方案中也有一个HTML网页。我正在将我的WebBrowser控件导航到该HTML页面。 HTML页面包含一个按钮。当用户单击该HTML页面上的该按钮时,如何调用C#函数?
答案 0 :(得分:4)
我从CEFSharp wiki获取此代码;希望这对你有帮助。
public class BoundObject
{
public void OnFrameLoadEnd (object sender, FrameLoadEndEventArgs e)
{
if(e.Frame.IsMain)
{
browser.ExecuteScriptAsync(@"
document.body.onmouseup = function()
{
bound.onSelected(window.getSelection().toString());
}
");
}
}
public void OnSelected(string selected)
{
MessageBox.Show("The user selected some text [" + selected + "]");
}
}
初始化CEF对象后,注册JavaScript对象:
var obj = new BoundObject();
browser.RegisterJsObject("bound", obj);
browser.FrameLoadEnd += obj.OnFrameLoadEnd;
答案 1 :(得分:2)
首先创建这个类;
public class BoundObject
{
public delegate void ItemClickedEventHandler(object sender, HtmlItemClickedEventArgs e);
public event ItemClickedEventHandler HtmlItemClicked;
public delegate void ItemResponseEventHandler(object sender, GetResponseEventArgs e);
public event ItemResponseEventHandler ItemResponse;
private ChromiumWebBrowser browser;
public BoundObject(ChromiumWebBrowser br) { browser = br; }
public void OnFrameLoadEnd(object sender, FrameLoadEndEventArgs e)
{
if (e.Frame.IsMain)
{
////browser.ExecuteScriptAsync(@"
//// document.body.onmouseup = function()
//// {
//// bound.onSelected(window.getSelection().toString());
//// }
////");
browser.EvaluateScriptAsync(@"window.onclick = function(e) { e.preventDefault(); bound.onClicked(e.target.outerHTML); }");
}
}
public void OnSelected(string selected)
{
MessageBox.Show("The user selected some text [" + selected + "]");
}
public void OnClicked(string id)
{
if (HtmlItemClicked != null)
{
HtmlItemClicked(this, new HtmlItemClickedEventArgs() { Id = id });
}
}
public void OnResponse(string projectId, string automationTaskId, string responseText)
{
if (ItemResponse != null)
{
ItemResponse(this, new GetResponseEventArgs() { AutomationTaskId = automationTaskId, ProjectId = projectId, ResponseText = responseText });
}
}
}
public class HtmlItemClickedEventArgs : EventArgs
{
public string Id { get; set; }
}
public class GetResponseEventArgs : EventArgs
{
public string ProjectId { get; set; }
public string AutomationTaskId { get; set; }
public string ResponseText { get; set; }
}
然后;
public frmProjectDetail()
{
InitializeComponent();
browser = new ChromiumWebBrowser("http://www.google.com.tr")
{
Dock = DockStyle.Fill,
};
//browser.RegisterJsObject("bound", bound);
splitContainerControl2.Panel1.Controls.Add(browser);
browser.LoadingStateChanged += OnLoadingStateChanged;
browser.ConsoleMessage += OnBrowserConsoleMessage;
browser.StatusMessage += OnBrowserStatusMessage;
browser.TitleChanged += OnBrowserTitleChanged;
browser.AddressChanged += OnBrowserAddressChanged;
browser.MouseClick += Browser_MouseClick;
var obj = new BoundObject(browser);
obj.HtmlItemClicked += Obj_HtmlItemClicked;
obj.ItemResponse += Obj_ItemResponse;
browser.RegisterJsObject("bound", obj);
browser.FrameLoadEnd += obj.OnFrameLoadEnd;
browser.DialogHandler = new SilentDialogHandler();
}
private void Obj_HtmlItemClicked(object sender, HtmlItemClickedEventArgs e)
{
this.InvokeOnUiThreadIfRequired(() => setMemoText("Tıklanan Element: " + e.Id));
}
也不要忘记实现在代码中声明的其他浏览器事件。当用户cliks任何html元素private void Obj_HtmlItemClicked触发器