Silverlight - 资源文件的路径 - ResourceHelper

Kan的blog 总结了如何在xaml中引用三种类型的文件:Resource, Content, None

 

对于引用Resource类型的文件,使用Assembly Unified Uri是最方便的, 写了一个helper来方便生产Images文件夹下的图片路径:

 

public class ResourceHelper
    {     
        private ResourceHelper() { }
        private static string _assemblyShortName;
        private static string GetAssemblyShortName()
        {
            if (_assemblyShortName == null)
            {
                Assembly a = typeof(ResourceHelper).Assembly;              
                _assemblyShortName = a.ToString().Split(',')[0];
            }
            return _assemblyShortName;
        }

        public  static string BuildImageResourcePath(string fileName)
        {
            //assuming the file is under folder "images"
            return string.Format("/{0};component/images/{1}", GetAssemblyShortName(), fileName);
        }
       
    }

 

代码中引用一个图片可以:

 img.Source = new System.Windows.Media.Imaging.BitmapImage(
                new Uri(ResourceHelper.BuildImageResourcePath("myicon.png"), UriKind.Relative));

 

 

你可能感兴趣的:(Silverlight - 资源文件的路径 - ResourceHelper)