6

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?

3 Answers 3

19

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; 
Sign up to request clarification or add additional context in comments.

5 Comments

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.
2

Unless I've not found the right part of Scott's post that you're looking at, he is binding image source to a url.

Specificaly,

ImageSource = tweet.Element("user").Element("profile_image_url").Value

Which would be something like

http://a0.twimg.com/profile_images/1158022545/mickn_normal.jpeg

Comments

0

Let me explain my solution regarding this topic. I just created a setting in the settings. Every configuration that you set here, will be shown in the App.config file too.

Settings

Settings2

After you've successfully configured the Image's path in the settings, you just have to get the source in the MainWindows.xaml.cs file.

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        string ImagePath = Properties.Settings.Default.ImagePath;
        Uri resourceUri = new Uri(@ImagePath,UriKind.RelativeOrAbsolute);
        MainImage.Source = new BitmapImage(resourceUri);            
    }
}

I hope this can help you a bit.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.