【发布时间】:2009-02-23 12:38:44
【问题描述】:
我通过 Silverlight 中的代码隐藏动态生成图像,显然图像源不接受字符串或 Uri 作为路径。
如何设置来源?
【问题讨论】:
-
我也花了一点时间才弄明白。 Guantam 的答案看起来像我使用的。
-
我不得不稍微改变一下,它可以在路径中不包含命名空间的情况下工作
标签: silverlight
我通过 Silverlight 中的代码隐藏动态生成图像,显然图像源不接受字符串或 Uri 作为路径。
如何设置来源?
【问题讨论】:
标签: silverlight
你的意思是它不会接受字符串作为源?
你不能这样做吗?
或者您是说您的图像在内存中并且您不知道如何引用它?
this.MyImage.Source = new BitmapImage(new Uri("/MyNameSpace;images/someimage.png", UriKind.Relative));
【讨论】:
// create a new image
Image image = new Image();
// better to keep this in a global config singleton
string hostName = Application.Current.Host.Source.Host;
if (Application.Current.Host.Source.Port != 80)
hostName += ":" + Application.Current.Host.Source.Port;
// set the image source
image.Source = new BitmapImage(new Uri("http://" + hostName + "/image111.jpg", UriKind.Absolute));
【讨论】:
我需要替换以下内容才能解决问题:
this.MyImage.Source = new BitmapImage(new Uri("/MyNameSpace;components/images/someimage.png", UriKind.Relative));
MyNameSpace 对我不起作用,但 ExecutingAssemblyName 对我有用,所以:
Dim tmp As String() = Assembly.GetExecutingAssembly.FullName.Split(","c)
Dim path As String = "/" & tmp(0) & ";component/images/"
MyImage.Source = new BitmapImage(new Uri(path & "someImage.png"))
【讨论】: