2

I am very new to programming and would love some help. I just installed MahApps.Metro and it looks really nice and nifty. But I cannot run the program without System.Windows.Interactivity.dll and MahApps.Metro.dll in the same folder, if I try running it without those two DLL's it just .. doesn't open. Is there a way to incorporate those DLL's in the exe or just run it without them?

Thanks in advance for any help!

4

2 回答 2

1

您可以使用 ILMerge ( http://www.microsoft.com/en-us/download/details.aspx?displaylang=en&id=17630 )。

使用 ILMerge,可以将所有程序集(文件夹中的所有 dll)合并到一个 exe 文件中。看到这个解释:http ://research.microsoft.com/en-us/people/mbarnett/ILMerge.aspx

但是部署所有程序集有什么问题。我认为这在当今很常见。

于 2013-04-24T06:53:51.323 回答
0

如果使用了这些程序集,您将无法运行您的应用程序。毕竟,您正在使用仅在这些程序集中的代码,如果您的应用程序除了关闭之外没有找到所需的代码,应该怎么办?

但是有一种方法可以将程序集嵌入到您的 exe 中,因此您只需重新分发一个文件。引用的程序集可以作为嵌入式资源添加(将 dll 添加到您的项目并将其设置"Build Action""Embedded Resource")并从那里加载。

然后,您需要自己加载它,AppDomain.AssemblyResolve以防当前AppDomain. 为此,您需要向此事件添加一个处理程序,如下所示:

AppDomain.CurrentDomain.AssemblyResolve += (sender, args) => {
    String resourceName = 
        "YourDefaultNameSpace." + new AssemblyName(args.Name).Name + ".dll";
    using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName)) {
        Byte[] assemblyData = new Byte[stream.Length];
        stream.Read(assemblyData, 0, assemblyData.Length);
        return Assembly.Load(assemblyData);
    }
};

对于 WPF 应用程序,您可以覆盖App.OnStartup (in App.xaml.cs) 并将其添加到那里:

protected override void OnStartup(StartupEventArgs e) {
    base.OnStartup(e);
    // ---- Add the handler code here ----
}

原始来源和更多详细信息:
Jeffrey Richter: Excerpt #2 from CLR via C#, Third Edition

于 2013-04-24T06:44:03.243 回答