以编程方式添加资产
Programmicatically Adding Assets
作为 CI 构建的一部分,我需要将 1 GB 的图像内容拉入我的项目文件中,以便将其编译到我的 appx 中。包含文件的数据和列表经常变化,所以不能预先包含在项目中。再加上庞大的规模将使我们的回购变得庞大。
除了破解 csproj 本身并添加一堆 xml 来执行此操作之外,有没有办法在构建时执行此操作?也许预构建中运行命令的东西?
所以,我在这上面花了很多时间,结果证明简单地使用预构建步骤是行不通的,因为你实际上必须修改 csproj 文件。编译后简单地使用 MakeAppx 将不起作用,编译过程实际上会更改您的 appxmanifest。
我最终编写了一个小型控制台应用程序,该应用程序在调用 MSBuild 之前在脚本中完成工作。我已将其包含在下面。
private static void AddDirectoryToProject(string sourceDir, Project project)
{
var dir = new DirectoryInfo(sourceDir);
FileInfo[] files = dir.GetFiles();
foreach (FileInfo file in files)
{
var virtualStart = file.FullName.IndexOf("sourcebinaries");
var virtualPath = file.FullName.Substring(virtualStart);
project.AddItemFast("Content", virtualPath, new List<KeyValuePair<string, string>>()
{
new KeyValuePair<string, string>("CopyToOutputDirectory", "PreserveNewest")
});
Console.WriteLine(virtualPath);
}
foreach (var subDir in dir.GetDirectories())
{
AddDirectoryToProject(subDir.FullName, project);
}
}
作为 CI 构建的一部分,我需要将 1 GB 的图像内容拉入我的项目文件中,以便将其编译到我的 appx 中。包含文件的数据和列表经常变化,所以不能预先包含在项目中。再加上庞大的规模将使我们的回购变得庞大。
除了破解 csproj 本身并添加一堆 xml 来执行此操作之外,有没有办法在构建时执行此操作?也许预构建中运行命令的东西?
所以,我在这上面花了很多时间,结果证明简单地使用预构建步骤是行不通的,因为你实际上必须修改 csproj 文件。编译后简单地使用 MakeAppx 将不起作用,编译过程实际上会更改您的 appxmanifest。
我最终编写了一个小型控制台应用程序,该应用程序在调用 MSBuild 之前在脚本中完成工作。我已将其包含在下面。
private static void AddDirectoryToProject(string sourceDir, Project project)
{
var dir = new DirectoryInfo(sourceDir);
FileInfo[] files = dir.GetFiles();
foreach (FileInfo file in files)
{
var virtualStart = file.FullName.IndexOf("sourcebinaries");
var virtualPath = file.FullName.Substring(virtualStart);
project.AddItemFast("Content", virtualPath, new List<KeyValuePair<string, string>>()
{
new KeyValuePair<string, string>("CopyToOutputDirectory", "PreserveNewest")
});
Console.WriteLine(virtualPath);
}
foreach (var subDir in dir.GetDirectories())
{
AddDirectoryToProject(subDir.FullName, project);
}
}