Search code examples
c#wpfwindows-phone-7embedded-resourcewindows-phone

Windows Phone 7: Check If Resource Exists


I need to check whether an embedded resource exists. In WPF this i relatively easy, but in WP7 I get a MissingManifestResourceException

The WPF code which works is:

public static IEnumerable<object> GetResourcePaths(Assembly assembly) { 
        var culture = System.Threading.Thread.CurrentThread.CurrentCulture; 
        //var resourceName = assembly.GetName().Name + ".g"; 
        var assemblyName = assembly.FullName.Split(',')[0]; 
        var resourceName = assemblyName + ".g"; 
        var resourceManager = new ResourceManager(assemblyName, assembly); 

        try { 
            var resourceSet = resourceManager.GetResourceSet(culture, true, true); 

            foreach (System.Collections.DictionaryEntry resource in resourceSet) { 
                yield return resource.Key; 
            } 
        } finally { 
            resourceManager.ReleaseAllResources(); 
        } 
    } 

I tried replacing it with the code below, which resulted in the exception (on line 9). Is there a way to do this in Silverlight / WP7?

public static IEnumerable<object> GetResourcePaths(Assembly assembly) { 
            var culture = System.Threading.Thread.CurrentThread.CurrentCulture; 
            //var resourceName = assembly.GetName().Name + ".g"; 
            var assemblyName = assembly.FullName.Split(',')[0]; 
            var resourceName = assemblyName + ".g"; 
            var resourceManager = new ResourceManager(assemblyName, assembly); 

            try { 
                var resourceSet = resourceManager.GetResourceSet(culture, true, true); 

                foreach (System.Collections.DictionaryEntry resource in resourceSet) { 
                    yield return resource.Key; 
                } 
            } finally { 
                resourceManager.ReleaseAllResources(); 
            } 
        } 

Solution

  • The answer to this previous question: WP7: collection of images seems to indicate that you might need to get a stream before calling GetResourceSet:

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

    Sounds a bit hacky, but if it works ;)