图像数据绑定XAML,Silverlight,C#,Windows Phone 7.1 [英] Image databinding XAML, Silverlight, C#, Windows Phone 7.1

查看:138
本文介绍了图像数据绑定XAML,Silverlight,C#,Windows Phone 7.1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 Windows Phone 7 编写应用程序,虽然我认为我了解如何数据绑定字段,但我无法将图像数据绑定到XAML元素。



这是我的XAML中我的图像元素的定义:

 < image Source = {Binding ImageLocation}> < / image> 

ImageLocation是一个字符串属性。由于某些原因,如果ImageLocation是指向Internet上的图像的字符串(例如http://whatever/image.jpg),则图像会显示,但如果图像存储在本地,则不显示任何内容在隔离存储上。



的确,图像位于隔离存储器中称为Images的文件夹中。因此,我已经尝试将ImageLocation属性设置为Images\image.jpg,但没有起作用。图像根本不显示。我也尝试了一些变体,如/Images/image.jpg,http://localhost/Images/image.jpg和各种其他奇怪的组合。



我也试图这样做:

 < image Source ={Binding Path = ImageLocation} > < / image> 

但是也没有。图像根本不显示,如果它是本地的,但它总是工作,如果它位于远程站点。



我已经看到了各种各样的示例在网上使用转换器显示图像。由于我是C#和Silverlight的新手,我不太明白为什么我需要一个转换器。据了解,转换器对于图像,将根据XAML元素所需要的(我可能是错误的)简单地返回字符串或实际的图像数据。但是,既然我事先知道一个字符串应该可以工作,为什么还要写这个转换器?



除了图像之外,我的所有其他字段都被正确绑定。 / p>

这里有一些关于我的代码设置的想法:



我在我的XAML页面中定义了一个ListBox元素, ItemTemplate和DataTemplate。在里面,我有一个两列的网格分别保存图像和文本字段。



除此之外,我有一个派生自IList的类,以支持延迟加载。此类具有表示列表框的每一行的ListItem列表。 ListItem具有定义图像的字符串属性和与它们相关联的一些文本。正如我所提到的,文本字段工作正常,只有ImageLocation属性指向Internet上的某个位置,而不是隔离存储上的位置,该映像才有效。



我已经使用了隔离存储资源管理器工具,并确保我正在加载的映像位于隔离存储上,因此不是不



感谢您的想法或意见。

解决方案

隔离存储文件只能以流的形式访问。如果要存储图像,则需要在绑定上使用转换器。这些将打开指定的文件,将流加载到内存中并返回位图图像源。



相对路径,如您的示例,可用于存储为Silverlight中的应用程序资源的图像DLL或托管网站资源(取决于主要的斜线和程序集名称等)。



这个答案有一个转换器,你可以尝试:来自IsolatedStorage的Windows Phone 7 Silverlight绑定映像


I'm writing an app for the Windows Phone 7 and although I think I understand how to databind fields, I am unable to databind an image to an XAML element.

This is how my image element is defined in my XAML:

<image Source="{Binding ImageLocation}" > </image>

ImageLocation is a string property. For some reason, my image shows up if "ImageLocation" is a string that points to an image on the Internet (e.g., "http://whatever/image.jpg") but it does not show anything if the image is stored locally on the Isolated Storage.

Indeed, the image is in a folder in Isolated Storage called "Images". Therefore, I have tried setting the ImageLocation property to "Images\image.jpg" but that didn't work. The image simply doesn't show up. I have also tried a bunch of variations such as "/Images/image.jpg", "http://localhost/Images/image.jpg", and all sorts of other odd combinations.

I have also attempted to do this:

<image Source="{Binding Path=ImageLocation}"> </image>

but that didn't work either. The image simply doesn't show up if it is local but it always works if it is located on a remote site.

I have seen various examples on the web using a "Converter" to display the image. As I am new to C# and Silverlight, I don't really understand why would I need a converter for this. As I understand it, the converter, for an image, would simply return either the string or the actual image data based on what the XAML element would require (I may be wrong). However, since I know beforehand that a string should work, why would I even have to write this converter ?

Besides the image, all my other fields are binded correctly.

Here's some idea about my code setup:

I am defining a ListBox element in my XAML page that has an ItemTemplate and a DataTemplate. Inside that, I have a grid with two columns that hold the image and the text fields respectively.

Other than that, I have a class derived from IList in order to support lazy loading. This class has a List of "ListItems" that represent each row of the listbox. A ListItem has string properties defining the image and some text associated with them. As I mentioned, the text fields work correctly and the image only works if the ImageLocation property points to a location on the internet, but not a location on the Isolated Storage.

I have checked with the Isolated Storage Explorer tool and I made sure that the image that I am trying to load is located on the Isolated Storage so it's not a matter of not finding the file.

Thanks for your ideas or comments.

解决方案

Isolated storage files are only accessible as streams. If you want to store your images there you will need to use a converter on the bindings. These will open the specified file, load the stream into memory and return a bitmap image source. They do not just return a filename.

Relative paths, such as your examples, are for use with images stored as either application resources in Silverlight DLLs, or hosting website resources (depending on leading slashes and assembly names etc).

This answer has a converter you can try: Windows Phone 7 Silverlight binding image from the IsolatedStorage

这篇关于图像数据绑定XAML,Silverlight,C#,Windows Phone 7.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆