Image databinding XAML, Silverlight, C#, Windows Phone 7.1

自闭症网瘾萝莉.ら 提交于 2019-12-11 12:09:07

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/7765842/image-databinding-xaml-silverlight-c-windows-phone-7-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!