在Silverlight中绑定图像时如何格式化URI?(How do I format a URI when binding an Image in Silverlight?)

编程入门 行业动态 更新时间:2024-10-28 17:18:44
在Silverlight中绑定图像时如何格式化URI?(How do I format a URI when binding an Image in Silverlight?)

我一直无法找到答案。

我有一个有图像路径的数据库(“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”的工作?

I haven't been able to find an answer to this.

I have a database that has image paths in it ("images/myimage.jpg"). These images exist on my asp.net site which is also where I host the SL. I want to bind these images to my ListBox control so that the image displays.

I have read that since I have a string value, "images/myimage.jpg", that I need to convert it to a BitMap image. I have done this:

The xaml:

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

The ImageConverter class:

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

I get an error when creating the URI, "The Format of the URI could not be determined". What am I doing wrong? If I create a Uri that looks like this: http://localhost:49723/images/myimage.jpg, it works just fine.

Why doesn't just "images/myimage.jpg" work?

最满意答案

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

一个技巧是将你的XAP移动到你的网站的根目录 ,所以媒体路径将相对于根目录。

在这里查看我的帖子在Silverlight中的相对URI。

Relative paths to media in Silverlight are wacky so they can work the same (wacky) way that WPF paths do. Relative paths are relative to the XAP file, not the app root.

One trick is to move your XAP to the root of your website, so media paths will be relative to the root.

See my post on relative URI's in Silverlight here.

更多推荐

images,myimage,jpg,图像,电脑培训,计算机培训,IT培训"/> <meta name="descrip

本文发布于:2023-07-08 01:03:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1070211.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   图像   Silverlight   URI   Image

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!