简体   繁体   English

WP7:图像收集

[英]WP7: collection of images

I have images in folder Images in my windows phone solution. 我的Windows Phone解决方案中的“图像文件夹”中有图像。 How can i get collection of images in this folder? 如何在此文件夹中收集图像? Build Action of all images is "Content". 所有图像的构建操作为“内容”。

It had been bugging me that it wasn't possible to do this so I've done a bit of digging and have come up with a way of getting a list of all image files with the build action of "Resource". 一直困扰着我,不可能做到这一点,所以我做了一些挖掘,并提出了一种使用“资源”的构建动作来获取所有图像文件列表的方法。 - Yes, this isn't quite what was asked for but hopefully this will still be useful. -是的,这不是要求的,但是希望它仍然有用。

If you really must use a build action of "Content" I'd use a T4 script to generate the list of files at build time. 如果您确实必须使用“内容”的生成操作,那么我将使用T4脚本在生成时生成文件列表。 (This is what I do with one of my projects and it works fine.) (这是我对一个项目所做的工作,并且工作正常。)

Assuming that the images are in a folder called "images" you can get them with the following: 假设图像位于名为“ images”的文件夹中,则可以通过以下方式获取它们:

var listOfImageResources = new StringBuilder();

var asm = Assembly.GetExecutingAssembly();

var mrn = asm.GetManifestResourceNames();

foreach (var resource in mrn)
{
    var rm = new ResourceManager(resource.Replace(".resources", ""), asm);

    try
    {
        var NOT_USED = rm.GetStream("app.xaml"); // without getting a stream, next statement doesn't work - bug?

        var rs = rm.GetResourceSet(Thread.CurrentThread.CurrentUICulture, false, true);

        var enumerator = rs.GetEnumerator();
        while (enumerator.MoveNext())
        {
            if (enumerator.Key.ToString().StartsWith("images/"))
            {
                listOfImageResources.AppendLine(enumerator.Key.ToString());
            }
        }
    }
    catch (MissingManifestResourceException)
    {
        // Ignore any other embedded resources (they won't contain app.xaml)
    }
}

MessageBox.Show(listOfImageResources.ToString());

This just displays a list of the names, but hopefully it'll be easy to change this to do whatever you need to. 这只是显示名称列表,但希望可以轻松更改此名称以执行所需的操作。

Any suggestions for improving this code will be greatly appreciated. 任何改进此代码的建议将不胜感激。

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

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