BrowserView.xaml.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using System;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. using System.Windows.Input;
  5. using Xilium.CefGlue.Common.Events;
  6. namespace Xilium.CefGlue.Demo.WPF
  7. {
  8. public partial class BrowserView : IDisposable
  9. {
  10. public BrowserView()
  11. {
  12. InitializeComponent();
  13. //browser.RegisterJavascriptObject(new BindingTestClass(), "boundBeforeLoadObject");
  14. }
  15. public event Action<string> TitleChanged;
  16. private void OnBrowserTitleChanged(object sender, string title)
  17. {
  18. TitleChanged?.Invoke(title);
  19. }
  20. private void OnBrowserLoadStart(object sender, LoadStartEventArgs e)
  21. {
  22. if (!e.Frame.IsMain)
  23. {
  24. return;
  25. }
  26. Application.Current.Dispatcher.BeginInvoke((Action) (() =>
  27. {
  28. addressTextBox.Text = e.Frame.Url;
  29. }));
  30. }
  31. private void OnAddressTextBoxKeyDown(object sender, KeyEventArgs e)
  32. {
  33. if (e.Key == Key.Enter)
  34. {
  35. browser.Address = ((TextBox)sender).Text;
  36. }
  37. }
  38. public async void EvaluateJavascript()
  39. {
  40. //Console.WriteLine(await browser.EvaluateJavaScript<string>("\"Hello World!\""));
  41. //Console.WriteLine(await browser.EvaluateJavaScript<int>("1+1"));
  42. //Console.WriteLine(await browser.EvaluateJavaScript<bool>("false"));
  43. //Console.WriteLine(await browser.EvaluateJavaScript<double>("1.5+1.5"));
  44. //Console.WriteLine(await browser.EvaluateJavaScript<double>("3+1.5"));
  45. //Console.WriteLine(await browser.EvaluateJavaScript<DateTime>("new Date()"));
  46. //Console.WriteLine(string.Join(", ", await browser.EvaluateJavaScript<object[]>("[1, 2, 3]")));
  47. //Console.WriteLine(string.Join(", ", (await browser.EvaluateJavaScript<ExpandoObject>("(function() { return { a: 'valueA', b: 1, c: true } })()")).Select(p => p.Key + ":" + p.Value)));
  48. }
  49. public void BindJavascriptObject()
  50. {
  51. //const string TestObject = "dotNetObject";
  52. //var obj = new BindingTestClass();
  53. //browser.RegisterJavascriptObject(obj, "dotNetObject");
  54. //var methods = obj.GetType().GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public)
  55. // .Where(m => m.GetParameters().Length == 0)
  56. // .Select(m => m.Name.Substring(0, 1).ToLowerInvariant() + m.Name.Substring(1));
  57. //var script = "(function () {" +
  58. // "let calls = [];" +
  59. // //string.Join("", methods.Select(m => $"calls.push({{ name: '{m}', promise: {TestObject}.{m}() }});")) +
  60. // $"calls.push({{ name: 'getObjectWithParams', promise: {TestObject}.getObjectWithParams(5, 'a string', {{ Name: 'obj name', Value: 10 }}, [ 1, 2 ]) }});" +
  61. // "calls.forEach(c => c.promise.then(r => console.log(c.name + ': ' + JSON.stringify(r))).catch(e => console.log(e)));" +
  62. // "})()";
  63. //browser.ExecuteJavaScript(script);
  64. }
  65. public void OpenDevTools()
  66. {
  67. browser.ShowDeveloperTools();
  68. }
  69. public void Dispose()
  70. {
  71. browser.Dispose();
  72. }
  73. }
  74. }