How to tell if an image is in my resources

JoeBoothJoeBooth USMember ✭✭

I wish add debug code check that an image exits (to help catch bugs when an image is not added to the project properly) - how would i do this?

ImageSource.FromFile("myImage.png") does not give any functions to help with this?

Posts

  • Josh_QuintusJosh_Quintus USMember ✭✭

    You can loop over the available resources. Here's a stack overflow article describing how: http://stackoverflow.com/questions/2041000/loop-through-all-the-resources-in-a-resx-file

  • JoeBoothJoeBooth USMember ✭✭

    @Josh_Quintus‌ - that does not seam compatible with Forms - i need to know if a file is in the android & iOS resource files structure - i want to read this from forms code (not from platform specific code)

  • Josh_QuintusJosh_Quintus USMember ✭✭
            string[] resourceNames = this.GetType().GetTypeInfo().Assembly.GetManifestResourceNames();
            foreach (var name in resourceNames)
            {
                // do something with Name
            }
    
  • CraigDunnCraigDunn USXamarin Team Xamurai

    This is specifically discussed in the Working with Files doc, although please note that is referring to files (including images) that have been added as embedded resources.

    That does not apply when using FromFile since that loads images from the platform-specific app. See the Working with Images doc for more info. You're right that there are no additional functions (like 'Exists') to help determine whether an image is there, unfortunately.

  • TeoTeo DKMember ✭✭

    Hmm, a more roundabout way of doing it is to wrap it in a try/catch block, and handle the exception. Maybe that works.

Sign In or Register to comment.