在Silverlight中绑定图像时如何格式化URI?

时间:2009-08-05 01:41:46

标签: asp.net image silverlight uri

我一直无法找到答案。

我有一个包含图像路径的数据库(“images / myimage.jpg”)。这些图像存在于我的asp.net网站上,这也是我托管SL的地方。我想将这些图像绑定到我的ListBox控件,以便显示图像。

我已经读过,因为我有一个字符串值“images / myimage.jpg”,我需要将它转换为BitMap图像。我这样做了:

xaml:

 <Image Source="{Binding ImageFile, Converter={StaticResource ImageConverter}}"/>

ImageConverter类:

    public object Convert(object value, Type targetType,
                                  object parameter, CultureInfo culture)
            {
                try
                {
                    Uri source= new Uri(value.ToString());
                    return new BitmapImage(source);
                }
                catch(Exception ex)
                {
                    return new BitmapImage();
                }
            }

创建URI时出现错误,“无法确定URI的格式”。我究竟做错了什么?如果我创建一个看起来像这样的Uri:http://localhost:49723/images/myimage.jpg,它就可以了。

为什么不只是“images / myimage.jpg”有效?

5 个答案:

答案 0 :(得分:5)

无论您的XAP文件位于何处,都可以使用简单的动态方法,类似于以下内容。

//Get the root path for the XAP
string src = Application.Current.Host.Source.ToString();

//Get the application root, where 'ClientBin' is the known dir where the XAP is
string appRoot = src.Substring(0,src.IndexOf("ClientBin")); 

//Create the image / uri
BitmapImage img = new BitmapImage();
img.UriSource = new Uri(appRoot + "/Images/myImage.png", UriKind.Relative);

这有帮助吗?

答案 1 :(得分:3)

Silverlight中媒体的相对路径很古怪,因此它们可以像WPF路径一样(古怪)工作。相对路径是相对于XAP文件而不是应用程序根目录。

一个技巧是move your XAP to the root of your website,因此媒体路径将相对于根。

请参阅Silverlight here中关于相对URI的帖子。

答案 2 :(得分:1)

今天我自己遇到了这个问题并按照Jon描述的方式修复了它(虽然没有看到你的帖子。本来可以节省我一些时间。)我还要指出使用过载可以解决具体的错误:

Uri source = new Uri("Path/Image.jpg", UriKind.Relative);

如果不移动XAP文件,您仍然无法访问images子目录,但它会解析错误消息。在那时,程序很高兴地返回一个没有内容的图像,让你使用Fiddler或Web Dev Helper来找出真正的问题。

答案 3 :(得分:1)

http://www.silverlightexamples.net/post/How-to-Get-Files-From-Resources-in-Silverlight-20.aspx

资源,并且永不复制图像,然后使用“SLapplicationName; component / mypathtoimage / image.png”

using System.Windows.Resources;      // StreamResourceInfo
using System.Windows.Media.Imaging;  // BitmapImage
....

StreamResourceInfo sr = Application.GetResourceStream(
    new Uri("SilverlightApplication1;component/MyImage.png", UriKind.Relative));
BitmapImage bmp = new BitmapImage();
bmp.SetSource(sr.Stream); 

答案 4 :(得分:0)

您只需编写一个方法,为您提供完整的服务器地址(protocol:// server:port /)并使用它创建绝对URL:

public class Helper{
    public static string ServerAddress{
        get{
            if (_server == "")
                _server = _ServerAddress();
            return _server;
        }
    }

     private static string _ServerAddress(){
        HttpContext ctx = HttpContext.Current;
        if (ctx == null) return "";

        HttpRequest request = ctx.Request;
        if (request == null) return "";

      string srvr = request.ServerVariables["SERVER_NAME"];
      string port = string.IsNullOrEmpty(request.ServerVariables["SERVER_PORT"])?
            "" : ":" + request.ServerVariables["SERVER_PORT"];
      string protocol = "http://";//request.ServerVariables["SERVER_PROTOCOL"];
      return string.Format("{0}{1}{2}{3}", protocol, srvr, port, 
                   request.ApplicationPath);
    }
}    

并更改转换器方法行:

Uri source= new Uri(value.ToString());

if(!string.IsNullOrEmpty(value.ToString()))
   Uri source= new Uri(Helper.WebAddress + value.ToString());