MainWindow.axaml.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using System;
  2. using System.IO;
  3. using Avalonia;
  4. using Avalonia.Controls;
  5. using Avalonia.Markup.Xaml;
  6. using JsonRpcLite.InProcess;
  7. using JsonRpcLite.Rpc;
  8. using Xilium.CefGlue;
  9. using Xilium.CefGlue.Avalonia;
  10. using Xilium.CefGlue.Common;
  11. using Xilium.CefGlue.Common.Handlers;
  12. namespace Flyinsono.Demo
  13. {
  14. public sealed class JsonRpcHandler<T> : IHostHandler
  15. {
  16. private readonly JsonRpcInProcessEngine _engine = new();
  17. private readonly JsonRpcServer _server = new();
  18. private readonly JsonRpcClient _client = new();
  19. public string Host { get; }
  20. public JsonRpcHandler(string host, T service)
  21. {
  22. Host = host;
  23. _server.UseEngine(_engine);
  24. _client.UseEngine(_engine);
  25. _server.RegisterService(service);
  26. _server.Start();
  27. }
  28. public HandleResult Handle(string path, byte[] postData = null)
  29. {
  30. try
  31. {
  32. var serviceName = path;
  33. if (serviceName.StartsWith("/"))
  34. {
  35. serviceName = serviceName.Substring(1);
  36. }
  37. var resultData = _client.ProcessAsync(serviceName, postData).Result;
  38. var resultStream = new MemoryStream(resultData);
  39. return new HandleResult(resultStream, "application/json");
  40. }
  41. catch
  42. {
  43. // ignored
  44. }
  45. return null;
  46. }
  47. }
  48. public class MainWindow : Window
  49. {
  50. public AvaloniaCefBrowser _browser;
  51. public MainWindow()
  52. {
  53. InitializeComponent();
  54. #if DEBUG
  55. this.AttachDevTools();
  56. #endif
  57. }
  58. private void InitializeComponent()
  59. {
  60. AvaloniaXamlLoader.Load(this);
  61. var browserContainer = this.FindControl<Border>("BrowserBorder");
  62. var flyinsonoHandler = new FileSystemHostHandler("app.flyinsono.com",Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "flyinsono.com"));
  63. var handler = new HostRequestHandler();
  64. handler.RegisterHostHandler(flyinsonoHandler);
  65. _browser = new AvaloniaCefBrowser
  66. {
  67. Address = "http://app.flyinsono.com/index.html",
  68. RequestHandler = handler,
  69. };
  70. _browser.Settings.WebSecurity = CefState.Disabled;
  71. _browser.ContextMenuHandler = new TextContextMenuHandler(_browser);
  72. browserContainer.Child = _browser;
  73. }
  74. }
  75. public class TextContextMenuHandler : ContextMenuHandler
  76. {
  77. private readonly BaseCefBrowser _owner;
  78. public TextContextMenuHandler(BaseCefBrowser owner)
  79. {
  80. _owner = owner;
  81. }
  82. protected override void OnBeforeContextMenu(CefBrowser browser, CefFrame frame, CefContextMenuParams state, CefMenuModel model)
  83. {
  84. if (!((state.ContextMenuType & CefContextMenuTypeFlags.Editable) != 0 || (state.ContextMenuType & CefContextMenuTypeFlags.Selection) != 0))
  85. {
  86. model.Clear();
  87. }
  88. base.OnBeforeContextMenu(browser, frame, state, model);
  89. }
  90. protected override bool RunContextMenu(CefBrowser browser, CefFrame frame, CefContextMenuParams parameters, CefMenuModel model, CefRunContextMenuCallback callback)
  91. {
  92. //This is for enable the style.
  93. _owner.HandleOpenContextMenu(parameters, model, callback);
  94. return true;
  95. }
  96. }
  97. }