简体   繁体   English

在代码后面设置WPF图像控件的源

[英]Setting the source of WPF Image control behind code

Recently I've been strugling with setting the Setting the source of WPF Image control inside code. 最近,我一直在努力在代码内部设置“设置WPF Image控件的来源”。

I've tried something like: 我已经尝试过类似的东西:

Image.Source = new BitmapImage(new Uri(@"/hrc.Hands;component/Images/BlueFolder.png", UriKind.RelativeOrAbsolute));

But it doesn't seem to work and thorws Object reference not set to an instance of an object exception. 但这似乎不起作用,并且没有将对象引用设置为对象异常的实例。 Here's the screenshot of the path to my image inside hierachy: 这是我的图像在hierachy中的路径的屏幕截图:

在此处输入图片说明

You can use.. 您可以使用..

var imageSource = new Uri(@"pack://application:,,,/" + Assembly.GetExecutingAssembly().GetName().Name + ";component/" + "Images/BlueFolder.png", UriKind.Absolute);
Image.Source = imageSource;

Also, make sure that BlueFolder.png is set as a Resource . 另外,请确保将BlueFolder.png设置为Resource Right click the image, and select Properties to do this. 右键单击图像,然后选择“ Properties以执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM