SilverLight动态指定Image的Source

来源:互联网 发布:初中生教学软件有哪些 编辑:程序博客网 时间:2024/06/07 20:05

在后台指定Image的source不是简单的传递一个路径的string就够了,目前所知,有两种方法:
第一种如MSDN所述:
Image myImage = new Image();
myImage.Source = new BitmapImage(new Uri("myPicture.jpg", UriKind.RelativeOrAbsolute));
URL(例如,http://contoso.com/myPicture.jpg)来设置 Source,也可以指定相对于应用程序的 XAP 文件的 URL。

第二种效率就较低了,但是在某些情况下还比较有用:
string xxx = "<Image xmlns=/"http://schemas.microsoft.com/winfx/2006/xaml/presentation/" Source=/""
                + imgPath + "/"></Image>";
Image ig = (Image)System.Windows.Markup.XamlReader.Load(xxx);
imgPath就是一个表示URL的string。