![](/img/trans.png)
[英]Load 60 images in stackpanel or Listbox from folder in windows phone 8
[英]Dynamically load images from project folder - Windows Phone 7
我想做的看起来很简单,我在其他平台上也做过……
这里有一些上下文:假设您有 1000 个小图像要显示在数据绑定的 ListBox 中。 您首先将项目中的图像包含到文件夹“/images”中。 您将他们的构建操作设置为“内容”。
现在的问题是:如何在运行时将所有这些图像动态加载到您的应用程序中? 通过动态,我的意思是不必知道 1000 张图像的每个名称。
(如果您正在考虑独立存储,我已经尝试过了。图像文件夹是您项目的一部分,但不会自动加载到独立存储中,因此据我所知,您不能从独立存储中加载图像)
您可以在设计时使用以下 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",
};
}
}
}
您显然可以根据需要更改部分 class 的名称空间和名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.