开发者

WP7: collection of images

开发者_如何学JAVA

I have images in folder Images in my windows phone solution. 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. (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:

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜