0

我正在将 scitersharp 与 WinForms 一起使用,并尝试构建一个独立的编译 c# 可执行文件。这是一个加载 HTML 文件的简单“Hello World”应用程序。最初编译后,bin\Debug 文件夹中生成了 3 个 DLL(sciter.dll、sciter64.dll 和 SciterSharpWindows.dll)。所以,我用谷歌搜索并试图找到将这些 dll 文件嵌入到可执行文件中的解决方案。这是我尝试过的:

  • 在 Visual Studio 的项目窗口中加载 DLL 并将构建选项设置为“嵌入式资源”

  • 添加对资源的引用,但此方法不起作用,它给了我以下错误:

    `A reference to resource 'pathto\sciter.dll' could not be added. Please make sure the file is accesible and that it is a valid assembly or COM component`
    
  • 阅读此stackoverflow 问题的解决方案

  • 安装 Cosutra.Fody 包并重建项目。这次我设法只嵌入了 SciterSharpWindows.dll

这是我的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sciter;
using SciterSharp;
using SciterSharp.WinForms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        // Create a class-accesible sciter control
        private SciterControl SciterElement;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // Create a new instance of the sciter control
            SciterElement = new SciterControl();
            // Set the callback once the element is ready to be used
            SciterElement.HandleCreated += SciterControl1_HandleCreated;

            // Add the Sciter Control to the Form and fill it
            this.Controls.Add(SciterElement);
            SciterElement.Dock = DockStyle.Fill;
            
        }

        private void SciterControl1_HandleCreated(object sender, EventArgs e)
        {
            // Initialize with some HTML
            SciterElement.SciterWnd.LoadHtml(@"
                <div style='padding: 0px 8px;'>
                <h1>
                    Hello World
                </h1>
                </div>
            ");
        }
    }
}

有没有办法将这些资源嵌入到可执行文件中?另外,如果我遗漏了什么,请告诉我?先感谢您。

4

1 回答 1

0

这是一个解决方法,也许您可​​以参考。

首先,将 dll 添加到 Reference 并设置Copy LocalFalse.

在此处输入图像描述

然后新建一个项目文件夹“libs”,将引用的dll复制到这个文件夹中,并设置Build Action为“Embedded Resources”

在此处输入图像描述

在 main() 方法中,添加处理异常“dll not found”的代码。

static void Main()
{
    AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;

    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new Form1());
}
private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs e)
{
    //The namespace of the project is embeddll, and the embedded dll resources are in the libs folder, so the namespace used here is: embeddll.libs.
    string _resName = "embeddll.libs." + new AssemblyName(e.Name).Name + ".dll";
    using (var _stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(_resName))
    {
        byte[] _data = new byte[_stream.Length];
        _stream.Read(_data, 0, _data.Length);
        return Assembly.Load(_data);
    }
}
于 2020-06-29T05:55:16.813 回答