Featured Posts

VS2010+SL3VS2010 beta + Silverlight 3 It keeps showing "Unable to Start Debugging. The Silverlight managed debugging package isn't installed".Finally the problem can be solved by installing Developer Runtime.

Readmore

An image in a post Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Quisque sed felis. Aliquam sit amet felis. Mauris semper, velit semper laoreet dictum, quam diam dictum urna, nec...

Readmore

Cash-Out Refinance For many, their homes are just not dwellings that protect them against rain, sun, and wind. But they are piggy banks, which can be used to raise some urgent money, even if...

Readmore

Sunday, June 1, 2008

0
Setting source of Images

This week end, I am so busy with my heavy work load... homework! But, I still want to share my experience on writeing Silverlight Application.

As I remembered, I have seen more than ten posts asking for the blinding of the source of their images.

Actually people want to make use of the images that they added to the project.

Honestly speaking, it has so many ways to do the trick.

First one:

[sourcecode language='csharp']Image Img = new Image();
BitmapImage bi3 = new BitmapImage();
bi3.UriSource = new Uri("pic.gif", UriKind.Relative);
Img.Source = bi3;
[/sourcecode]

Let me explain a little bit. If you added the image to your project, it is better for you to use UriKind.Relative. Just like the word "Relative" it is taking the resources relatively to the project. But how about somethings that are not added to the project?

[sourcecode language='csharp']Image Img = new Image();
BitmapImage bi3 = new BitmapImage();
bi3.UriSource = new Uri("http://localhost/pic.gif", UriKind.Absolute);
Img.Source = bi3;
[/sourcecode]



Apart from setting it programmatically with C#, you can also add it with xaml code throught C#.

[sourcecode language='csharp']Image img = XamlReader.Load("xmlns=\"http://schemas.microsoft.com/client/2007\"
xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\ " / > " ) as Image;
Layoutroot.children.add(img);
[/sourcecode]




It is rare to see people using XamlReader, but it can really do the trick!

Live with Light
Steve Wong (Hong Kong)

0 comments:

Post a Comment