在其他 C# 项目中,例如 Window Forms
和 WPF
,我只需将构建的 exe 复制到其他 .net 环境即可运行,不会出现错误。
但是,在MonoGame
中,exe文件需要大量的dll,并且需要content文件夹才能成功运行。
有没有办法将dll和内容包含在exe中?
最佳答案
其实只需2步就可以实现。
- 嵌入 dll。
- 包括内容。
嵌入 dll
这可以通过安装 Costura.Fody 轻松实现使用 NuGet。
在包管理器控制台中键入以下行
Install-Package Costura.Fody
来源:Embedding DLLs in a compiled executable .
包含内容
- 将编译的内容文件(
.xnb
文件和其他内容)复制到Content
文件夹。 - 转到项目属性页面并将编译的内容文件添加到您的资源中。
替换下面的代码:
public Game1() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; }
与
public Game1() { graphics = new GraphicsDeviceManager(this); ResourceContentManager resxContent; resxContent = new ResourceContentManager(Services, Resources.ResourceManager); Content = resxContent; }
完成了!您现在可以使用旧方法加载内容
Content.Load<Texture2D>("Image1");
关于c# - 将 dll 和内容嵌入到 monogame 的 exe 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41908427/