• 基于.Net CEF 实现 Vue 等前端技术栈构建 Windows 窗体应用


    Tip:Focus

    If you have decided to work on a task, do it as well as you can. Don’t start multiple things at the same time. Do only one thing at one time. You’ll not become quicker, just you work multithreaded. If you work multithreaded you’ll become exhausted, make more errors and lose time to jump from one task to another. If you code, code. This is not only about programming, this is a general tip.

    零、参考资料

    1、https://github.com/cefsharp/CefSharp/wiki/Quick-Start-For-MS-.Net-5.0-or-greater

    2、https://github.com/cefsharp/CefSharp/wiki/Quick-Start

    3、https://github.com/cefsharp/CefSharp/wiki/General-Usage#javascript-integration

    一、安装 Nuget 包

    https://www.nuget.org/packages/CefSharp.WinForms

    安装 CefSharp.WinForms 包后会显示 Readme.txt,

    二、配置项目

    1. <PropertyGroup Condition="'$(PlatformTarget)' == 'x64'">
    2. <RuntimeIdentifier Condition="'$(RuntimeIdentifier)' == ''">win-x64RuntimeIdentifier>
    3. <SelfContained Condition="'$(SelfContained)' == ''">falseSelfContained>
    4. PropertyGroup>

    三、加载远程网页示例

    使用 ChromiumWebBrowser 加载百度首页,

    1. // Form1.cs
    2. using CefSharp;
    3. using CefSharp.WinForms;
    4. using System.Windows.Forms;
    5. namespace CefWindowsFormsApp
    6. {
    7. public partial class Form1 : Form
    8. {
    9. private static ChromiumWebBrowser browser;
    10. public Form1()
    11. {
    12. InitializeComponent();
    13. AddChromiumWebBrowser();
    14. }
    15. ///
    16. /// Create a new instance in code or add via the designer
    17. ///
    18. private void AddChromiumWebBrowser()
    19. {
    20. browser = new ChromiumWebBrowser("www.baidu.com");
    21. this.Controls.Add(browser);
    22. }
    23. private void Form1_Load(object sender, System.EventArgs e)
    24. {
    25. // Load a url
    26. browser.LoadUrl("https://www.baidu.com/");
    27. }
    28. }
    29. }

    这这个示例中,我们引入了 CefSharp 库,在 Form1 窗体中添加了浏览器控件 ChromiumWebBrowser ,并且在窗体启动时加载百度首页,

    四、加载本地网页示例

    首先创建一个 Vue 项目,

    1. # 使用 Vite
    2. cnpm create vite@latest
    3. # cd vite-project
    4. # cnpm i

    接着完成前端的开发之后,打包静态资源,

    npm run build

    然后在 WinForm 项目下创建 Resources 文件夹,把前端打包的 dist 文件夹下的文件全部复制过来,并且文件属性设置为“嵌入的资源”,

    最后通过 RegisterScheme 注册为本地资源访问,

    1. // Form1.cs
    2. using CefSharp;
    3. using CefSharp.SchemeHandler;
    4. using CefSharp.WinForms;
    5. using System.Windows.Forms;
    6. namespace CefWindowsFormsApp
    7. {
    8. public partial class Form1 : Form
    9. {
    10. private static ChromiumWebBrowser browser;
    11. public Form1()
    12. {
    13. InitializeComponent();
    14. AddChromiumWebBrowser();
    15. }
    16. ///
    17. /// Create a new instance in code or add via the designer
    18. ///
    19. private void AddChromiumWebBrowser()
    20. {
    21. InitBrowser();
    22. browser = new ChromiumWebBrowser("http://cefsharp.test");
    23. this.Controls.Add(browser);
    24. }
    25. public static void InitBrowser()
    26. {
    27. // Pseudo code; you probably need more in your CefSettings also.
    28. var settings = new CefSettings();
    29. settings.RegisterScheme(new CefCustomScheme
    30. {
    31. SchemeName = "http",
    32. DomainName = "cefsharp.test",
    33. SchemeHandlerFactory = new FolderSchemeHandlerFactor
  • 相关阅读:
    阿里直呼真省钱!全网首发IntelliJ IDEA应用实战手册竟遭哄抢
    微服务架构介绍
    Linux防火墙Centos6的常用命令iptables
    c# 调用巴斯勒相机 进行图像识别
    MindSpore论文解读 | 文本语义哈希在大规模信息检索系统的应用
    <二>自己实现简单的string
    JetBrains ToolBox修改应用安装位置
    数据结构——排序
    蓝桥杯每日一题2023.10.7
    Docker 容器里安装ssh和连接ssh
  • 原文地址:https://blog.csdn.net/weixin_47560078/article/details/133974513