Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在C中设置字符串作为图像源#_C#_Silverlight_Visual Studio 2010_Windows Phone 7 - Fatal编程技术网

C# 在C中设置字符串作为图像源#

C# 在C中设置字符串作为图像源#,c#,silverlight,visual-studio-2010,windows-phone-7,C#,Silverlight,Visual Studio 2010,Windows Phone 7,我在我的WindowsPhone7应用程序中找到了几种方法来解决这个问题,但我似乎找不到任何有效的方法。让我困惑的是,我以前做过类似的事情,没有任何问题,所以我不知道为什么它不起作用。导致我出现问题的代码如下: if(appSettings.Contains(“图像”)) myImage.Source=(字符串)appSettings[“image”]; 其他的 myImage.Source=“default.jpg” 我得到的错误是 无法将类型“string”隐式转换为“System.Wind

我在我的WindowsPhone7应用程序中找到了几种方法来解决这个问题,但我似乎找不到任何有效的方法。让我困惑的是,我以前做过类似的事情,没有任何问题,所以我不知道为什么它不起作用。导致我出现问题的代码如下:
if(appSettings.Contains(“图像”)) myImage.Source=(字符串)appSettings[“image”]; 其他的 myImage.Source=“default.jpg”

我得到的错误是

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


这让我感到困惑的原因是,我这样做了,您直接将图像源绑定到字符串。那么我该怎么做才能解决这个问题呢?

除非我没有找到Scott文章中您正在查看的正确部分,否则他正在将图像源绑定到url

具体来说,

ImageSource = tweet.Element("user").Element("profile_image_url").Value
大概是


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

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

让我解释一下我对这个话题的解决方案。我刚刚在设置中创建了一个设置。您在此处设置的每个配置也将显示在App.config文件中

在设置中成功配置映像路径后,只需在MainWindows.xaml.cs文件中获取源文件

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);            
    }
}

我希望这能对你有所帮助

这就是你如何将url应用到图像源属性的方法非常感谢你们,问题解决了!如果我对字符串和URI还有什么基本的误解,请告诉我。再次感谢@Dan如果要将字符串绑定到ImageSource的Source属性,可以使用转换器进行绑定-请看一个示例。您的转换器将接收一个字符串并返回一个位图图像-如果有人想插手,可能有更好的方法来实现这一点!我想我可能需要回到基础上来,确切地理解字符串的定义。例如,Mick似乎建议url不能是字符串?我只是认为它是一个“字符串”,将它们注入源字段与将它们注入TextBlock的文本字段没有什么不同。当您使用XAML中的字符串在图像上指定源时,它实际上使用隐式转换器将其转换为ImageSource。与超链接按钮中的URL相同,等等。