解决反射加载Xap包中的图片路径问题
2010-08-27 09:40
537 查看
图片路径问题
最近做的silverlight项目框架设计是将每个模块都做成单独的silverlight项目,这些单独项目生成的Xap包通过在父silverlight程序中
反射实现加载。在实际开发过程中,用blend添加的图片默认路径是"\123.jpg”。编译后图片会做为内容一起打包到Xap中。在图片路径中
以"\"开始的路径,silverlight运行时会从父silverlight Xap包中搜索文件,然后显示。而反射加载的xap包中的图片会找不到路径。
解决方法
解决此问题比较简单,我们需要将反射xap包中的图片设置为资源,然后将图片路径"\123.jpg”修改为"123.jpg”。
在图片路径中如果没有"\",silverlight运行时将会从资源中搜索图片。
xap包反射加载类
代码
最近做的silverlight项目框架设计是将每个模块都做成单独的silverlight项目,这些单独项目生成的Xap包通过在父silverlight程序中
反射实现加载。在实际开发过程中,用blend添加的图片默认路径是"\123.jpg”。编译后图片会做为内容一起打包到Xap中。在图片路径中
以"\"开始的路径,silverlight运行时会从父silverlight Xap包中搜索文件,然后显示。而反射加载的xap包中的图片会找不到路径。
解决方法
解决此问题比较简单,我们需要将反射xap包中的图片设置为资源,然后将图片路径"\123.jpg”修改为"123.jpg”。
在图片路径中如果没有"\",silverlight运行时将会从资源中搜索图片。
xap包反射加载类
代码
public class XapLoader { public static UserControl LoadStream(Stream e, string MainPage, string MainDll) { string appMainfest = new StreamReader(Application.GetResourceStream( new StreamResourceInfo(e, null) , new Uri("AppManifest.xaml", UriKind.Relative)).Stream ).ReadToEnd(); XElement xe = (XDocument.Parse(appMainfest)).Root; var results = from deploymentPart in xe.Elements().Elements() where deploymentPart.Attributes("Source").Count() > 0 select deploymentPart.Attribute("Source").Value; Assembly asse = null; foreach (var deploymentpart in results) { string source = deploymentpart; StreamResourceInfo streamInfo = Application.GetResourceStream(new StreamResourceInfo(e, "application/binary"), new Uri(source, UriKind.Relative)); AssemblyPart assP = new AssemblyPart(); if (source == MainDll) { asse = assP.Load(streamInfo.Stream); } else { assP.Load(streamInfo.Stream); } } return asse.CreateInstance(MainPage) as UserControl; } public void loadUri(Uri xapuri, string MainPage, string MainDll) { WebClient wc = new WebClient(); wc.OpenReadCompleted += new OpenReadCompletedEventHandler((s, e) => { string appMainfest = new StreamReader(Application.GetResourceStream( new StreamResourceInfo(e.Result, null) , new Uri("AppManifest.xaml", UriKind.Relative)).Stream ).ReadToEnd(); XElement xe = (XDocument.Parse(appMainfest)).Root; var results = from deploymentPart in xe.Elements().Elements() where deploymentPart.Attributes("Source").Count() > 0 select deploymentPart.Attribute("Source").Value; Assembly asse = null; foreach (var deploymentpart in results) { string source = deploymentpart; StreamResourceInfo streamInfo = Application.GetResourceStream(new StreamResourceInfo(e.Result, "application/binary"), new Uri(source, UriKind.Relative)); AssemblyPart assP = new AssemblyPart(); if (source == MainDll) { asse = assP.Load(streamInfo.Stream); } else { assP.Load(streamInfo.Stream); } } UIElement uie = asse.CreateInstance(MainPage) as UIElement; if (OnLoadComplete != null) OnLoadComplete(this, new XapEventArgs() { XapRootUIElement = uie }); }); wc.OpenReadAsync(xapuri); } public event EventHandler<XapEventArgs> OnLoadComplete; } public class XapEventArgs : EventArgs { public UIElement XapRootUIElement { get; set; } }
相关文章推荐
- spring boot 本地图片不能加载(图片路径)的问题及解决方法
- 解决vue的 v-for 循环中图片加载路径问题
- android获取项目下的一张图片的绝对路径问题以及解决方法
- iOS开发 - 完美解决SDWebImage加载多个图片内存崩溃的问题
- 解决图片按顺序加载问题
- 解决从相册获取图片路径cursor = getContentResolver().query(uri, proj, null, null, null)报空的问题
- Android使用BitmapFactory.Options解决加载大图片内存溢出问题
- Android使用UIL加载圆角图片不显示的问题及解决办法
- [android]完美解决ListView加载网络图片乱跳问题
- 解决4.4版本以上剪裁图片时提示无法加载图片的问题
- 解决Glide加载网络圆角图片先显示完整图片的问题
- 循环获取加载图片的时候,加载的图片每次都是最后一个图片,如果解决这个问题。
- ListView异步加载图片--图片缓存和错位问题解决方案
- android解决加载大图片卡顿和oom问题。
- 解决Scaleform中使用AS2.0的UILoader不加载图片的问题
- j2me加载图片的路径问题
- universal image loader在listview/gridview中滚动时重复加载图片的问题及解决方法
- 解决在vue项目中,发版之后,背景图片报错,路径不对的问题
- Android有效解决加载大图片时内存溢出的问题 .
- 有效解决Android加载大图片时内存溢出的问题