简体   繁体   English

从Xamarin Forms中的资源加载所有图像

[英]Load all images from resource in Xamarin Forms

I'm creating an offline application in Xamarin Forms, I'm currently saving images in specific projects (Android and iOS) and would like to load them all into a ListView. 我正在Xamarin Forms中创建一个脱机应用程序,当前正在将图像保存在特定项目(Android和iOS)中,并希望将它们全部加载到ListView中。 I tried using the following code: 我尝试使用以下代码:

IEnumerable <string> images = Directory.EnumerateFiles("Resources", "*.jpg", SearchOption.AllDirectories); 

But I was unsuccessfully, could anyone help me please? 但是我失败了,有人可以帮我吗?

Thank you. 谢谢。

if you want to do like this,i suggest you to save the images into Assets folder,and you could iterate the folder and filter with .png . 如果您想这样做,建议您将图像保存到Assets文件夹中,然后可以迭代该文件夹并使用.png进行过滤。

for example in Android : 例如在Android中

List<System.IO.Stream> streamList = new List<System.IO.Stream>();
AssetManager assetManager = Assets;
string[] images = assetManager.List("");
for (int i = 0; i < images.Length; i++)
   {
     if (images[i].EndsWith(".png"))
       {
          using (StreamReader sr = new StreamReader(assetManager.Open(images[i])))
             {
               streamList.Add(sr.BaseStream);
             }
       }
   }

Assets in android: android中的资产:

在此处输入图片说明

you could get the list of image stream by DependencyService or MessagingCenter 您可以通过DependencyServiceMessagingCenter获取图像流列表

I was able to solve my problem with the following code: 我可以使用以下代码解决我的问题:

public IEnumerable<string> EnumerateImagesFromResource(Func<FieldInfo, bool> filter)
{
    return typeof(Resource.Drawable).GetFields().Where(filter).Select(s => s.Name);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM