zoukankan      html  css  js  c++  java
  • Silverlight图片相对路径的设置[转]

    【原文来自于http://blogs.msdn.com/b/xusun/archive/2008/12/31/silverlight.aspx

    这个问题也困扰了我很久.经常在Project里包含一些图片,然后在应用里指定路径,结果就是Load不上,遇到ImageError的错误。

     

    这里列出一些常用的设置Image 相对路径的几种方法:

    假设我有这样一个solution,包括: MyImageLibrary,其中包含一个文件夹"Images\MyImage1.png"; MyLoadImage是我的Silverlight Application里面也有一个Images\MyImage2.png, 页面中有一Image控件来分别显示这两个图片 

    那么有如下几种选择:

    1. Build Action="Resource", Copy To OutPut Directory = "Do not copy".

     

    这时如果需要用另一个项目中的Image,那么需要这样设置Image路径

    xaml: <Image Source="/MyImageLibrary;component/Images/MyImage1.png" />

    code: myImage.Source = new BitmapImage(new Uri("/MyImageLibrary;component/Images/MyImage1.png", UriKind.Relative));

    这个Uri有如下3部分:

    • /MyImageLibrary 是Image所在的Assembly的名字,前面的"/"是必须的。
    • ";component/"是Assembly名字和在此Assembly下具体路径之间的分隔符,必须要有。
    • "Images/MyImage1.png" 就是所在Assembly中的路径啦。

    此时图片是作为资源被嵌入进Assembly的. 如果用Reflector 打开MyImageLibrary.dll,就能够找到这个资源:

    这是Image在另一个assembly的时候的用法,如果在同一个项目下,那就可以直接这样用:

    <Image Source="Images/MyImage2.png"/>

    2. Build Action = "Content", Copy To OutPut Directory = "Do not copy".

    这种情况,如果图片在另一个Project中,那是没有办法获得的。只有在同一个Project中的时候,才能够被找到,因为这种设置会把Image嵌入.xap中.如果打开生成的xap,会看到如下:

    可以看到只有同一个Project中的Image作为Content嵌入到xap中。

    这时的用法就是

    <Image Source="Images/MyImage2.png"/>

    3.Build Action = "None", Copy To OutPut Directory = "Copy Always".

    如果不希望图片被压进Xap文件,而是用户调用的时候再动态Load,那可以用这种方法。这种方法图片会被复制到 xap所在路径下,路径依然是 Images\MyImage1.png, Images\MyImage2.png.

    使用方法如前<Image Source="Images/MyImage1.png" /> 或<Image Source="Images/MyImage2.png"/>

    遇到图片加载错误时,应该先检查选用的是那种方法,然后再检查图片是否被正确放在所期望的地方,dll/xap/路径 中。

    其他常见的原因还包括:用绝对路径的时候要注意是否是Cross-scheme, 比如如果你的项目要从加载位于 http://www.mydomain.com/Myimage.png的图片,但是如果你Debug是在浏览器中打开的是file://C:/MyProject/..那就属于是cross-scheme (File vs. Http),这时会有Security错误产生,解决方法就是用一个ASP.net website 作为起始工程,然后加入Silverlight 工程。

  • 相关阅读:
    Security and Cryptography in Python
    Security and Cryptography in Python
    Security and Cryptography in Python
    Security and Cryptography in Python
    Security and Cryptography in Python
    Security and Cryptography in Python
    Security and Cryptography in Python
    微信小程序TodoList
    C语言88案例-找出数列中的最大值和最小值
    C语言88案例-使用指针的指针输出字符串
  • 原文地址:https://www.cnblogs.com/zhangronghua/p/SilverLightImagePath.html
Copyright © 2011-2022 走看看