温馨提示:本文翻译自stackoverflow.com,查看原文请点击:silverlight - Setting String as Image Source in C#
c# silverlight visual-studio-2010 windows-phone-7

silverlight - 在C#中将字符串设置为图像源

发布于 2020-06-08 12:57:13

好吧,我在Windows Phone 7应用程序中查找了几种方法来解决此问题,但似乎找不到任何有效的方法。让我感到困惑的是,我之前所做的事情没有问题,所以我不确定为什么它不起作用。引起我问题的代码是这样的:
if(appSettings.Contains(“ image”))myImage.Source =(string)appSettings [“ image”]; else myImage.Source =“ default.jpg”;

我得到的错误是

无法将类型'string'隐式转换为'System.Windows.Media.ImageSource。

这让我感到困惑的原因是因为我完成了此Twitter应用程序教程,在该教程中,您将图像源直接绑定到字符串。那我该怎么解决呢?

查看更多

提问者
Dan
被浏览
26
thomasmartinsen 2010-12-31 07:26

从代码执行操作时,需要指定ImageSource而不是字符串:

Uri uri = new Uri("...", UriKind.Absolute); 
ImageSource imgSource = new BitmapImage(uri); 
myImage.Source = imgSource;