Windows 通用 - 显示来自资产的图像
本文关键字:图像 通用 显示 Windows | 更新日期: 2023-09-27 18:34:45
我正在尝试滚动浏览应用程序中的图像,但是我无法弄清楚如何填充我的列表。图像使用1.jpg以上的数字命名。如果有人能帮忙,那就太好了。
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语句的情况下执行此操作,但是在测试下一个文件是否存在时它会崩溃::(
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";
Uri uri1 = new Uri(path + "''" + testFile);
if (await appFolder.TryGetItemAsync(uri1.ToString()) != null)
{
test = 99999;
}
}
while (test != 99999);
}
您的列表不包含任何项目。您的 foreach 永远不会运行,因为您的列表中不会有条目。
您需要遍历 myImageFolder-root 中的所有路径并将这些 uri 添加到列表中,然后您可以在 foreach 中使用它们来创建图像并为列表中的每个 uri 设置其来源。
此外,不需要图像编号,因为您将拥有 URI。
首先通过遍历文件夹来准备 URI 列表。然后修改现有的 foreach 以使用这些对象来构建图像对象。
另外,不要在迭代时添加到集合中...
我有这个工作,不需要一个foreach
:D感谢@Richard埃里克森
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);
}