TestForm.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using System.Windows.Forms;
  2. using Cbdx.Tests.CefRelated;
  3. using Cbdx.Tests.Services;
  4. using CefSharp;
  5. using CefSharp.WinForms;
  6. namespace Cbdx.Tests
  7. {
  8. public partial class TestForm : Form
  9. {
  10. private readonly DataExchangeDispatcherBuilder _dataExchange;
  11. private ChromiumWebBrowser _browser;
  12. public TestForm()
  13. {
  14. InitializeComponent();
  15. _dataExchange = new DataExchangeDispatcherBuilder();
  16. _dataExchange.RegisterService<IWindowControlService>(new WindowControlService(this));
  17. }
  18. private void DemoForm_Shown(object sender, System.EventArgs e)
  19. {
  20. CreateOrReloadCefBrowserControl();
  21. }
  22. public void CreateOrReloadCefBrowserControl()
  23. {
  24. if (null != _browser)
  25. {
  26. MainPanel.Controls.Remove(_browser);
  27. _browser.Dispose();
  28. }
  29. _browser = new ChromiumWebBrowser(CefInit.ResourceRootUrl + "index.html");
  30. _dataExchange.RegisterService<ICefDevService>(new CefDevService(_browser), true);
  31. _browser.RegisterJsObject(nameof(Cbdx), _dataExchange.Dispatcher, new BindingOptions { CamelCaseJavascriptNames = false });
  32. _browser.Dock = DockStyle.Fill;
  33. MainPanel.Controls.Add(_browser);
  34. _browser.BringToFront();
  35. }
  36. private void F12ToolStripButton_Click(object sender, System.EventArgs e)
  37. {
  38. _browser.ShowDevTools();
  39. }
  40. private void ReloadToolStripButton_Click(object sender, System.EventArgs e)
  41. {
  42. CreateOrReloadCefBrowserControl();
  43. }
  44. }
  45. }