从.cs文件更改图像源

问题描述:

如何从.cs文件更改图像的当前源?我已经试过这样:从.cs文件更改图像源

file.xaml.cs:

BitmapImage yesImage = new BitmapImage(new Uri("/Test;component/Images/yes.png")); // new source address 
img.Source = yesImage; // update source of img 

XAML文件有一个控制<image Name="img" />居住在它。

+0

这应该工作 - 假设图像存在于指定的位置。究竟发生了什么? – ChrisF

我现在工作了。我将图像的“构建动作”更改为“内容”,并且由于图像对于我移除的“/ Test; component /”项目而言是本地的,并且我还向URI对象添加了UriKind.Relative。

BitmapImage yesImage = new BitmapImage(
     new Uri("/Images/yes.png", UriKind.Relative)); // new source address 
img.Source = yesImage; // update source of img