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

Sco*_*ttG 3 asp.net silverlight uri image

我一直无法找到答案.

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

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

xaml:

 <Image Source="{Binding ImageFile, Converter={StaticResource ImageConverter}}"/>
Run Code Online (Sandbox Code Playgroud)

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();
                }
            }
Run Code Online (Sandbox Code Playgroud)

创建URI时出现错误,"无法确定URI的格式".我究竟做错了什么?如果我创建一个看起来像这样的Uri:http:// localhost:49723/images/myimage.jpg,它运行得很好.

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

Por*_*key 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);
Run Code Online (Sandbox Code Playgroud)

这有帮助吗?