2009-08-05 57 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);