【问题标题】:compiling/merging dll into standalone exe with wpf使用 wpf 将 dll 编译/合并到独立的 exe 中
【发布时间】:2011-07-06 10:05:39
【问题描述】:

背景:Merging dlls into a single .exe with wpf

如何将 .dll 引用合并到 .exe 文件中,我阅读了上面的帖子,得到了背后的原理,但我不知道该怎么做?(我是新手,对不起) 参考文件是 HtmlagilityPack.dll

目前我的 App.xaml.cs 包含:

public partial class App : Application
    {
       public App(){
            AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(ResolveAssembly);

            // proceed starting app...
        }

        static Assembly ResolveAssembly(object sender, ResolveEventArgs args)
        {
            //We dont' care about System Assembies and so on...
            if (!args.Name.ToLower().StartsWith("Html")) return null;

            Assembly thisAssembly = Assembly.GetExecutingAssembly();

            //Get the Name of the AssemblyFile
            var name = args.Name.Substring(0, args.Name.IndexOf(',')) + ".dll";

            //Load form Embedded Resources - This Function is not called if the Assembly is in the Application Folder
            var resources = thisAssembly.GetManifestResourceNames().Where(s => s.EndsWith(name));
            if (resources.Count() > 0)
            {
                var resourceName = resources.First();
                using (Stream stream = thisAssembly.GetManifestResourceStream(resourceName))
                {
                    if (stream == null) return null;
                    var block = new byte[stream.Length];
                    stream.Read(block, 0, block.Length);
                    return Assembly.Load(block);
                }
            }
            return null;
        }
    }

我还应该在哪里进行更改?,我一直在尝试使用 http://blog.mahop.net/post/Merge-WPF-Assemblies.aspx 的示例过去一个小时,但无法弄清楚如何使用 HtmlAgilityPack 进行更改。

【问题讨论】:

  • 错误是什么?你的应用发生了什么?
  • 应用程序编译,但 dll 文件不包含在 exe 中,当我尝试将 .exe 作为独立运行(从文件夹中删除 HtmlAgilityPack.dll)时,它退出并说明一些错误。我尝试在上面的代码中设置断点,我的程序从不输入上面的代码。为什么?
  • 我认为您应该更仔细地调试此应用程序的初始加载,并查看 AssemblyResolve 事件正在寻找哪些其他程序集。这一行很可能是您问题的关键: if (!args.Name.ToLower().StartsWith("Html")) return null; -- 加载 htmlAgilitypack 时,您可能需要加载其他依赖项。您不应该简单地返回 null,任何“AssemblyResolve”的执行都意味着当前 AppDomain 正在寻找引用。
  • 但是代码永远不会进入ResolveAssembly Block!!。我还要在 App.xaml 中添加一些东西吗??
  • 知道了。您可以主动加载 HtmlAgilityPack.dll 程序集吗?而不是将负载推迟到“解决”处理程序中?我知道让该事件触发的唯一方法是在编译时引用“HtmlAgilityPack.dll”,构建,然后删除 DLL。

标签: c# .net wpf dll


【解决方案1】:

好的,终于不得不使用 SmartAssembly 程序了。 但仍在寻找通过代码实现的解决方案。

【讨论】:

【解决方案2】:

你的代码看起来有点不对劲,应该更像这样:

public class App : Application
{
    [STAThreadAttribute()]
    public static void Main()
    {
        AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(ResolveAssembly);
        // etc...
    }

    // etc...

然后您还需要更改项目属性页面中的“启动对象”设置以使用App 类(即上面的代码) - 然后您应该看到此类的Main 方法是开始调试时执行的第一个代码。

【讨论】:

    猜你喜欢
    • 2011-04-08
    • 1970-01-01
    • 1970-01-01
    • 2015-03-28
    • 2013-08-03
    • 1970-01-01
    • 2012-12-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多