I have an array of strings
images[]={"1.png","2.png","n...png"};
I am trying to set the sou开发者_如何学JAVArce of my image to a image in the array.
myImage.Source=images[2];
I get the following error.
Cannot implicitly convert type 'string' to 'System.Windows.Media.ImageSource
What am I doing wrong here?
Well, the error message seems reasonable clear to me. Image.Source
is of type ImageSource
, not string
. So in order to assign the Source
property, you need an ImageSource
. For example:
myImage.Source = new BitmapImage(new Uri(images[2], UriKind.Relative));
(ImageSource
is an abstract class; in practice BitmapImage
is the concrete class you'll usually want to use.)
In the code you cant set the string path to the source. Image.Source is expecting an Media.ImageSource which means you need to explicity create a BitMapImage and assign to the Source.
myImage.Source = new BitmapImage(new Uri(images[2], UriKind.Relative));
精彩评论