微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐