`
ribishuangba
  • 浏览: 288190 次
文章分类
社区版块
存档分类
最新评论

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

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics