C# Windows Universal-显示资源中的图像
我正试图在我的应用程序中滚动浏览图像,但我无法确定如何填充我的列表。这些图像使用1.jpg以上的数字命名。如果有人能帮忙,那就太好了C# Windows Universal-显示资源中的图像,c#,list,listview,win-universal-app,C#,List,Listview,Win Universal App,我正试图在我的应用程序中滚动浏览图像,但我无法确定如何填充我的列表。这些图像使用1.jpg以上的数字命名。如果有人能帮忙,那就太好了 async private void Exec() { // Get the file location. StorageFolder appFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
async private void Exec()
{
// Get the file location.
StorageFolder appFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
string myImageFolder = (appFolder.Path + "\\Assets\\Images");
int imageNumber = 1;
List<Uri> fileList = new List<Uri>();
foreach (var fileItem in fileList)
{
string imageFileName = imageNumber + ".jpg";
Uri uri = new Uri(myImageFolder + "/" + imageFileName);
fileList.Add(uri);
image.Source = new BitmapImage(new Uri(uri.ToString()));
await Task.Delay(TimeSpan.FromSeconds(1));
imageNumber++;
}
}
您的列表不包含任何项目。您的foreach将永远不会运行,因为您的列表中将没有条目 您需要遍历myImageFolder根目录中的所有路径并将这些uri添加到列表中,然后您可以在foreach中使用它们为列表中的每个uri创建图像并设置其源 此外,imageNumber是不需要的,因为您将有URI 通过遍历文件夹,首先准备URI列表。然后修改现有的foreach以使用它们来构建图像对象
另外,在迭代集合时不要将其添加到集合中 我有这个功能,不需要一个foreach:D谢谢@Richard erikson
async private void Exec()
{
// Get the file location.
string root = Windows.ApplicationModel.Package.Current.InstalledLocation.Path;
string path = root + @"\Assets\Images";
StorageFolder appFolder = await StorageFolder.GetFolderFromPathAsync(path);
int imageNumber = 1;
int test = imageNumber;
do
{
string imageFileName = imageNumber + ".jpg";
Uri uri = new Uri(path + "\\" + imageFileName);
image.Source = new BitmapImage(new Uri(uri.ToString()));
await Task.Delay(TimeSpan.FromSeconds(1));
test = imageNumber + 1;
imageNumber++;
string testFile = test + ".jpg";
if (await appFolder.TryGetItemAsync(testFile) != null)
{
test = 99999;
}
else
{
test = 1;
}
}
while (test == 99999);
}
谢谢Richard,对不起,我有点困惑。我是否需要一个foreach语句来填充列表,然后再另一个语句来显示图像?我尝试在没有foreach的情况下执行此操作,因为我无法让它工作@Richard eriksson基本上是的。使用一次迭代,根据在目录中找到的每个URI文件创建图像对象列表。然后再次迭代这个列表并绘制它们。如果图像存储在您的项目中,您可以使用URI$ms-appx:///Assets/Images.Thanks 但是我将从Azure中删除并下载它们,所以我不知道代码运行时会有多少。
async private void Exec()
{
// Get the file location.
string root = Windows.ApplicationModel.Package.Current.InstalledLocation.Path;
string path = root + @"\Assets\Images";
StorageFolder appFolder = await StorageFolder.GetFolderFromPathAsync(path);
int imageNumber = 1;
int test = imageNumber;
do
{
string imageFileName = imageNumber + ".jpg";
Uri uri = new Uri(path + "\\" + imageFileName);
image.Source = new BitmapImage(new Uri(uri.ToString()));
await Task.Delay(TimeSpan.FromSeconds(1));
test = imageNumber + 1;
imageNumber++;
string testFile = test + ".jpg";
if (await appFolder.TryGetItemAsync(testFile) != null)
{
test = 99999;
}
else
{
test = 1;
}
}
while (test == 99999);
}