您可以用下面的T4模板在设计时得到这样的:
<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ output extension=".gen.cs" #>
<#@ import namespace="System.IO"#>
// <auto-generated />
using Microsoft.Phone.Controls;
namespace MyAppNamespace
{
public partial class MainPage : PhoneApplicationPage
{
private static string[] AllFilesInImagesFolder()
{
return new[] {
<#
DirectoryInfo directoryInfo = new DirectoryInfo(Path.Combine(Path.GetDirectoryName(Host.TemplateFile), "images"));
foreach(FileInfo file in directoryInfo.GetFiles("*.*", SearchOption.AllDirectories))
{
if (!file.FullName.Contains(@"\."))
{#>
"<#= file.FullName.Substring(file.FullName.IndexOf("images")).Replace(@"\", "/") #>",
<# }
}
#>
};
}
}
}
它会产生这样的:
// <auto-generated />
using Microsoft.Phone.Controls;
namespace MyAppNamespace
{
public partial class MainPage : PhoneApplicationPage
{
private static string[] AllFilesInImagesFolder()
{
return new[] {
"images/image1.png",
"images/image2.png",
"images/image3.png",
"images/image4.png",
"images/image5.png",
};
}
}
}
呦你可以明显改变命名空间和部分类的名称,只要你适合。
图像是否具有命名模式或是随机的?另外,如果你愿意,你可以将来自XAP的图像传输到你的独立存储中,但在这种情况下可能是不必要的http://stackoverflow.com/questions/6244150/how-to-rename-an-image/6246037#comment- 7283927 – keyboardP 2011-06-14 14:14:28
我喜欢这个建议,但我不知道图像的名字。我想你可以说他们是随机的。没有与他们如何命名相关的模式。我正在寻找的东西就像 - Directory.GetFiles(/ images)...这将让枚举给定目录中的所有文件;你可以在运行时加载它们......但Directory.GetFiles在wp7上被禁止 – 2011-06-14 14:31:01
我不认为有一种方法可以在不知道内容文件名的情况下迭代XAP文件。它也不能在运行时被修改,所以你必须在编译时添加图像。你有没有理由不知道图像名称?还没有尝试过,但可以将所有图像设置为“资源”并使用“ResourceManager”。但是,这样做会增加应用程序的加载时间。 – keyboardP 2011-06-14 14:36:46