我是一名初学者,刚开始使用 Silverlight,并有一个非常基础的问题。我想要显示一个 .png 图像。我已经在 page.xaml 文件中完成了这个操作,但我想在代码(C#)中完成,这样我就可以在程序运行时添加和移除图像。我看到了一些代码,其中你将图像添加到 Canvas 的 Children 中,但是当我这样做时,没有任何图像被显示出来。能否提供一些代码以及放置代码的位置?这是我一直在使用的代码。没有异常,但也没有图像出现。
图像被设置为“资源”和“不可复制”。
page.myCanvas.Children.Add(LoadImage("Image/MrBlue"));
public Image LoadImage(string resource)
{
Image img = new Image();
Uri uri = new Uri(resource, UriKind.Relative);
ImageSource imgSrc = new System.Windows.Media.Imaging.BitmapImage(uri);
img.SetValue(Image.SourceProperty, imgSrc);
return img;
}
图像被设置为“资源”和“不可复制”。