WPF:“类型的值”String“无法转换为”System.Windows.Media.ImageSource“。 [英] WPF: "Value of type 'String' cannot be converted to 'System.Windows.Media.ImageSource'."

查看:1409
本文介绍了WPF:“类型的值”String“无法转换为”System.Windows.Media.ImageSource“。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



XAML 的作品:

 < Image Name =ImageThing
Source =images / Thing.png/>

Visual Basic 失败:

  ImageThing.Source =images / Thing.png

…这个异常


String类型的值不能为
转换为
'System.Windows.Media.ImageSource'。


如何创建System.Windows.Media我需要的图像资源?






更新



改编自 MSDN示例的作品:

  Dim bmp As New BitmapImage()
bmp.BeginInit()
bmp.UriSource = New Uri(图像/ Thing.png,UriKind.Relative)
bmp.EndInit()
ImageThing.Source = bmp


解决方案

WPF使用隐式类型转换器将xaml字符串转换为预期类型。在代码中,您被静态绑定的对象类型...如果您查看这里的示例,它显示了如何将源属性设置为从本地uri以编程方式生成的BitmapImage。


I'm trying to set a WPF Image's source.

XAML works:

<Image Name="ImageThing"
       Source="images/Thing.png"/>

Visual Basic fails:

ImageThing.Source = "images/Thing.png"

…with this exception:

Value of type 'String' cannot be converted to 'System.Windows.Media.ImageSource'.

How do I create the System.Windows.Media.ImageSource that I need?


Update

This code adapted from an MSDN example works:

Dim bmp As New BitmapImage()
bmp.BeginInit()
bmp.UriSource = New Uri("images/Thing.png", UriKind.Relative)
bmp.EndInit()
ImageThing.Source = bmp

解决方案

WPF uses an implicit type converter to convert the xaml string to the expected type. In code you are statically bound by the object type... If you look at the example here it shows how to set the source property to a BitmapImage that is generated from a local uri programatically.

这篇关于WPF:“类型的值”String“无法转换为”System.Windows.Media.ImageSource“。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆