2009-08-05 31 views
3

我一直未能找到答案。如何在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”的工作?

回答

3

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

一招是move your XAP to the root of your website,所以媒体路径将相对于根。

请参阅我在Silverlight的相对URI上发表的文章here

+0

谢谢乔恩,我希望我可以有我的过去3小时回:(这是有道理的,我用在我提出我的XAP文件的办法。我也要去看看由下面的动态方法PortageMonkey。 – ScottG 2009-08-05 14:18:17

0

你可以简单的写,让你完整的服务器地址的方法(协议://服务器:端口/),并用它来创建绝对网址:

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

这有帮助吗?

+0

Oooh。良好的调用。如果您无法移动XAP文件,可能也是访问其他资源的有用技巧。 – Raumornie 2009-08-05 03:37:34

1

今天刚刚遇到了这个问题,我自己修复了上面Jon的描述(虽然没有看到你的帖子,但可以节省一些时间)。我还要指出,具体的错误可以通过使用超载:

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

你会仍然无法在不移动XAP文件来访问图片子目录,但它解决了错误消息。那时,程序只是愉快地返回一个没有内容的图像,让你使用Fiddler或Web Dev Helper找出真正的问题。

1

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

资源,并且绝不能拷贝的图像上,然后用“SLapplicationName;组件/ mypathtoimage /图像。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); 
相关问题