开发者

How to set uri for local image in silverlight app?

In SL class library MyLib, I have a image say my.png. Then I want to use it in code behind I tried following way:

StreamResourceInfo resourceStream = Application.GetResourceStream(new Uri("/MyL开发者_运维知识库ib;component/my.png", UriKind.Relative));    
BitmapImage image = new BitmapImage();                                
image.SetSource(resourceStream.Stream);
this.MyIcon.Source = image;   

But it's not woking. I think it's the Uri not set correctly. Help please.


This works:-

BitmapImage image = new BitmapImage(new Uri("/MyLib;component/my.png", UriKind.Relative));
MyIcon.Source = image;

I can't see why you would want to use a Stream here. Having said that your Stream code should work. The build action on the png should be "Resource" and "MyLib" in your Uri should be the Assembly name of the library as found on the "Silverlight" tab of the project properties.


Do you have your image marked as "Resource" in the properties window, or "Content"?


You could always set a style as a resource in your application and then call it like:

Application.Current.Resources["myCoolStyle"] and apply that to the image.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜