Windows Phone 7:检查资源是否存在

时间:2011-04-11 13:52:36

标签: c# wpf windows-phone-7 embedded-resource windows-phone

我需要检查是否存在嵌入式资源。在WPF这我相对容易,但在WP7中我得到一个MissingManifestResourceException

有效的WPF代码是:

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(); 
        } 
    } 

我尝试用下面的代码替换它,这导致异常(在第9行)。有没有办法在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(); 
            } 
        } 

1 个答案:

答案 0 :(得分:0)

上一个问题的答案:WP7: collection of images似乎表明您可能需要在调用 GetResourceSet 之前获取流:

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

听起来有点hacky,但如果有效;)