在其他C#项目中,例如Window Forms
和WPF
,我可以将构建的exe文件复制到其他具有.net的环境中,并且运行时没有错误。
但是,在MonoGame
中,exe文件需要很多dll,并且需要内容文件夹才能成功运行。
有没有办法在exe中包含dll和内容?
答案 0 :(得分:2)
实际上,它可以通过两个步骤来实现。
嵌入dll
使用NuGet安装Costura.Fody可以轻松实现这一目标。
在Package Manager控制台中键入以下行
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");