2009-02-23 74 views
30

我在Silverlight中通过代码隐藏动态生成图像,显然图像源不接受字符串或Uri作为路径。如何在Silverlight中设置Image.Source(后面的代码)

如何设置来源?

+0

这花了我一点时间才弄清楚。关塔姆的答案看起来像我所用的。 – BenMaddox 2009-02-23 17:56:12

+0

虽然我不得不改变它,但它没有在路径中包含名称空间 – Drahcir 2009-02-23 18:41:03

回答

53

你怎么看它不接受一个字符串作为源?

你无法做到这一点吗?

或者你是说你的形象是在记忆中,你不知道如何引用它?

this.MyImage.Source = new BitmapImage(new Uri("/MyNameSpace;images/someimage.png", UriKind.Relative)); 
+1

Does not接受字符串,我的意思是例如:MyImage.Source =“/ MyNameSpace; images/images.source.php”像在asp.net – Drahcir 2009-02-23 13:47:22

6
// 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)); 
1

我需要替换以下得到解决工作:

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")) 
相关问题