当前位置:   article > 正文

Avalonia学习(十七)-CEF_avalonia 网页加载

avalonia 网页加载

今天开始继续Avalonia练习。

本节:CefNet

1.引入

CefNet.Avalonia.Eleven

2.项目引入

Program中加入

  1. using Avalonia;
  2. using Avalonia.ReactiveUI;
  3. using Avalonia.Threading;
  4. using CefNet;
  5. using System;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Threading.Tasks;
  9. namespace CefAvalonia
  10. {
  11. internal sealed class Program
  12. {
  13. internal static CefAppImpl? app;
  14. private static DispatcherTimer? messagePump;
  15. private const int messagePumpDelay = 10;
  16. // Initialization code. Don't use any Avalonia, third-party APIs or any
  17. // SynchronizationContext-reliant code before AppMain is called: things aren't initialized
  18. // yet and stuff might break.
  19. [STAThread]
  20. public static void Main(string[] args)
  21. {
  22. string cefPath = GetProjectPath(PlatformInfo.IsMacOS);
  23. bool externalMessagePump = args.Contains("--external-message-pump");
  24. if (PlatformInfo.IsMacOS)
  25. {
  26. externalMessagePump = true;
  27. }
  28. var settings = new CefSettings();
  29. settings.MultiThreadedMessageLoop = !externalMessagePump;
  30. settings.ExternalMessagePump = externalMessagePump;
  31. settings.NoSandbox = true;
  32. settings.WindowlessRenderingEnabled = true;
  33. settings.LocalesDirPath = Path.Combine(cefPath, PlatformInfo.IsMacOS ? "Resources" : "locales");
  34. settings.ResourcesDirPath = Path.Combine(cefPath, PlatformInfo.IsMacOS ? "Resources" : "");
  35. settings.LogSeverity = CefLogSeverity.Warning;
  36. settings.UncaughtExceptionStackSize = 8;
  37. App.FrameworkInitialized += App_FrameworkInitialized;
  38. App.FrameworkShutdown += App_FrameworkShutdown;
  39. app = new CefAppImpl();
  40. app.ScheduleMessagePumpWorkCallback = OnScheduleMessagePumpWork;
  41. app.Initialize(cefPath, settings);
  42. BuildAvaloniaApp()
  43. .StartWithClassicDesktopLifetime(args);
  44. }
  45. private static string GetProjectPath(bool isMacOS)
  46. {
  47. return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".cefnet", "Release", isMacOS ? Path.Combine("cefclient.app", "Contents", "Frameworks", "Chromium Embedded Framework.framework") : "");
  48. }
  49. private static void App_FrameworkInitialized(object? sender, EventArgs e)
  50. {
  51. if (CefNetApplication.Instance.UsesExternalMessageLoop)
  52. {
  53. messagePump = new DispatcherTimer(TimeSpan.FromMilliseconds(messagePumpDelay), DispatcherPriority.Normal, (s, e) =>
  54. {
  55. CefApi.DoMessageLoopWork();
  56. Dispatcher.UIThread.RunJobs();
  57. });
  58. messagePump.Start();
  59. }
  60. }
  61. private static void App_FrameworkShutdown(object? sender, EventArgs e)
  62. {
  63. messagePump?.Stop();
  64. }
  65. private static async void OnScheduleMessagePumpWork(long delayMs)
  66. {
  67. await Task.Delay((int)delayMs);
  68. Dispatcher.UIThread.Post(CefApi.DoMessageLoopWork);
  69. }
  70. // Avalonia configuration, don't remove; also used by visual designer.
  71. public static AppBuilder BuildAvaloniaApp()
  72. => AppBuilder.Configure<App>()
  73. .UsePlatformDetect()
  74. .WithInterFont()
  75. .LogToTrace()
  76. .UseReactiveUI();
  77. }
  78. }

APP中加入

  1. using Avalonia;
  2. using Avalonia.Controls.ApplicationLifetimes;
  3. using Avalonia.Markup.Xaml;
  4. using CefAvalonia.ViewModels;
  5. using CefAvalonia.Views;
  6. using System;
  7. namespace CefAvalonia
  8. {
  9. public partial class App : Application
  10. {
  11. public static event EventHandler? FrameworkInitialized;
  12. public static event EventHandler? FrameworkShutdown;
  13. public override void Initialize()
  14. {
  15. AvaloniaXamlLoader.Load(this);
  16. }
  17. public override void OnFrameworkInitializationCompleted()
  18. {
  19. if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
  20. {
  21. desktop.MainWindow = new MainWindow
  22. {
  23. DataContext = new MainWindowViewModel(),
  24. };
  25. }
  26. base.OnFrameworkInitializationCompleted();
  27. }
  28. }
  29. }

添加一个实现处理类

  1. using CefNet;
  2. using System;
  3. using System.Runtime.InteropServices;
  4. namespace CefAvalonia
  5. {
  6. internal class CefAppImpl:CefNetApplication
  7. {
  8. protected override void OnBeforeCommandLineProcessing(string processType, CefCommandLine commandLine)
  9. {
  10. base.OnBeforeCommandLineProcessing(processType, commandLine);
  11. if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
  12. {
  13. commandLine.AppendSwitch("no-zygote");
  14. commandLine.AppendSwitch("no-sandbox");
  15. }
  16. }
  17. public Action<long> ScheduleMessagePumpWorkCallback { get; set; }
  18. protected override void OnScheduleMessagePumpWork(long delayMs)
  19. {
  20. ScheduleMessagePumpWorkCallback(delayMs);
  21. }
  22. }
  23. }

窗口后台

  1. using Avalonia.Controls;
  2. using CefNet.Avalonia;
  3. namespace CefAvalonia.Views
  4. {
  5. public partial class MainWindow : Window
  6. {
  7. public MainWindow()
  8. {
  9. InitializeComponent();
  10. WebView webview = new() { Focusable = true };
  11. Content = webview;
  12. webview.BrowserCreated += (s, e) => webview.Navigate("https://www.baidu.com");
  13. webview.DocumentTitleChanged += (s, e) => Title = e.Title;
  14. Closing += (s, e) => Program.app?.Shutdown();
  15. }
  16. }
  17. }

最后,下载对应的库

CEF Automated Builds (spotifycdn.com)

运行效果

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小舞很执着/article/detail/889316
推荐阅读
相关标签
  

闽ICP备14008679号