更改图像元素的来源

时间:2011-11-09 16:56:23

标签: c# windows-phone-7

我尝试更改image1的图片。在收集中我有“1.png”和“2.png”。

image1的属性中,Source为“/Appname ;component/pictures/1.png”

我写道:

image1.Source = "/Appname;component/pictures/2.png";

但它当然不起作用,因为“无法从字符串隐式转换为System.Windows.Media.ImageSource”。

如何将image1中的图片更改为“2.png”?

1 个答案:

答案 0 :(得分:1)

您无法直接指定string作为图像来源。但您可以使用BitmapImage作为来源。

示例:

BitmapImage bitmap = new BitmapImage(new Uri("/Appname;component/pictures/2.png", UriKind.Relative));
image1.Source = bitmap;

如果这不起作用,那么您的Uri(图片路径)出现问题,您应该查看Zannjaminderson提供的链接(在评论中)。

相关问题