所以我正在构建一个工具,它可以即时创建一个 ContentProject 然后构建它(以便它输出 Xnb) 目前的问题是,如果用CopyToOutputDirectory = 'always' 或 'PreserveNewest'。如果我们在 .contentProj 中查看不应该构建但应该复制的文件的该部分,则看起来像这样
<ItemGroup>
<None Include="MyFile.file">
<Name>level1</Name>
<Importer>XmlImporter</Importer>
<Processor>PassThroughProcessor</Processor>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
但是,我正在即时构建内容项目,因此我需要以下代码来创建项目并添加项目
var projectPath = Path.Combine(buildDirectory, "content.contentproj");
_projectRootElement = ProjectRootElement.Create(projectPath);
_projectRootElement.AddImport("$(MSBuildExtensionsPath)\\Microsoft\\XNA Game Studio\\v4.0\\Microsoft.Xna.GameStudio.ContentPipeline.targets");
_contentProject = new Project(_projectRootElement);
_contentProject.SetProperty("XnaPlatform", "Windows");
_contentProject.SetProperty("XnaProfile", "HiDef");
_contentProject.SetProperty("XnaFrameworkVersion", "v4.0");
_contentProject.SetProperty("Configuration", "Debug");
_contentProject.SetProperty("OutputPath", _outputDirectory);
// Register any custom importers or processors.
foreach (string pipelineAssembly in PipelineAssemblies)
{
_contentProject.AddItem("Reference", pipelineAssembly);
}
// Hook up our custom error logger.
_errorLogger = new ErrorLogger();
_buildParameters = new BuildParameters(ProjectCollection.GlobalProjectCollection)
{
Loggers = new ILogger[] { _errorLogger, }
};
//.... removed code that is not required the following is code that adds each item to the project. In case of items that shoulndt compile I m using None (as in xml from project above)
var itemType = compile ? "Compile" : "None";
var items = _contentProject.AddItem(itemType, filename);
var item = items.SingleOrDefault(x => x.EvaluatedInclude.Equals(filename, StringComparison.InvariantCultureIgnoreCase));
item.SetMetadataValue("Link", Path.GetFileName(filename));
item.SetMetadataValue("Name", Path.GetFileNameWithoutExtension(filename));
if (!compile)
item.SetMetadataValue("CopyToOutputDirectory", "Always");
最后是构建代码
BuildManager.DefaultBuildManager.BeginBuild(_buildParameters);
var request = new BuildRequestData(_contentProject.CreateProjectInstance(), new string[0]);
var submission = BuildManager.DefaultBuildManager.PendBuildRequest(request);
var execute = Task.Factory.StartNew(() => submission.ExecuteAsync(null, null), cancellationTokenSource.Token);
var endBuild = execute.ContinueWith(ant => BuildManager.DefaultBuildManager.EndBuild());
endBuild.Wait();
在 BuildRequest 中,空数组接受作为目标的参数......我已经经历了许多不同的目标,它们做不同的事情,从构建但没有真正输出文件到将依赖 dll 复制到主文件夹但不是我需要的
干杯