Well I've looked up several methods to fix this in my Windows Phone 7 app but I can't seem to find anything that works. What confuses me is that I've done something just like this before with no problem, so I'm not sure why it's not working. The code causing me the problem is this:
if (appSettings.Contains("image"))
myImage.Source = (string)appSettings["image"];
else
myImage.Source = "default.jpg";
The error I get is this
Cannot implicitly convert type 'string' to 'System.Windows.Media.ImageSource.
The reason this confuses me is because I did this Twitter app tutorial, in which you bind the image source directly to a string. So what can I do to fix this?
You need to specify an ImageSource instead of a string when doing it from code:
Uri uri = new Uri("...", UriKind.Absolute);
ImageSource imgSource = new BitmapImage(uri);
myImage.Source = imgSource;
and this is how you apply the url to the Image Source property
Thank you both very much, problem solved! If there is anything else fundamental about strings and URIs that I seem to be misunderstanding please let me know. Again, thanks!
@Dan If you're looking to bind a string to an ImageSource's Source property, you can do so using a Converter - have a look at msdn.microsoft.com/en-us/library/… for an example. Your converter would take in a string and return a BitmapImage - there might be a better way of doing this, if someone out there wants to chip in!
I think I perhaps need to go back to the basics and understand exactly what a string can be defined as. For example, Mick seemed to suggest that an url cannot be a string? I was just thinking of it as a "string of characters" and that injecting them into an Source field would be no different than injecting them into a TextBlock's text field.
When you specify Source on an Image using a string in XAML it actually uses an implicit converter to convert it to an ImageSource. Same with URL's in HyperlinkButtons and so on.