在 Silverlight 中绑定图像时如何格式化 URI?
我一直无法找到这个问题的答案。
我有一个数据库,其中包含图像路径(“images/myimage.jpg”)。 这些图像存在于我的 asp.net 站点上,该站点也是我托管 SL 的地方。 我想将这些图像绑定到我的 ListBox 控件,以便显示图像。
我读过,因为我有一个字符串值“images/myimage.jpg”,所以我需要将其转换为位图图像。 我已经这样做了:
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< /a>,它工作得很好。
为什么“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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
http://www.silverlightexamples.net/post/How-to-Get-Files-From-Resources-in-Silverlight-20.aspx" silverlightexamples.net/post/How-to-Get-Files-From-Resources-in-Silverlight-20.aspx
资源,并且切勿在图像上复制,然后使用“SLapplicationName;component/mypathtoimage/image.png ”
http://www.silverlightexamples.net/post/How-to-Get-Files-From-Resources-in-Silverlight-20.aspx
Resource, and Never Copy on the image, then use "SLapplicationName;component/mypathtoimage/image.png"
今天我自己就遇到了这个问题,并按照乔恩上面描述的方式修复了它(虽然没有看到你的帖子。本来可以节省我一些时间。)我还指出,可以通过使用重载来解决特定错误:
你'如果不移动 XAP 文件,d 仍然无法访问图像子目录,但它解决了错误消息。 此时,程序只是愉快地返回一个没有内容的图像,让您使用 Fiddler 或 Web Dev Helper 来找出真正的问题。
Just ran into this problem today myself and fixed it the way Jon describes above (without seeing your post though. Could have saved me some time.) I'd also point out that the specific error can be resolved by using the overload:
You'd still be unable to access the images subdirectory without moving the XAP file, but it resolves the error message. At that point the program just happily returns an image with no content, leaving you to use Fiddler or Web Dev Helper to figure out the real problem.
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.
您可以简单地编写一个方法,为您提供完整的服务器地址(协议://服务器:端口/)并使用它来创建绝对URL:
并将Converter方法行:更改
为
You can simply write a method that gives you full server address (protocol://server:port/) and use it to create absolute URLs:
and change you Converter method line:
to
无论 XAP 文件位于何处,都可以使用一种简单的动态方法,类似于以下内容。
这有帮助吗?
A simple, dynamic approach that will work regardless of where your XAP file is located is similar to the following.
Does this help?