Errr, some catchy title.... RSS 2.0
 Friday, May 04, 2007

There are examples how to set image to the Image object in Silverlight (both in 1.0 and 1.1); however I haven’t found one how to set it to any object that can be filled. The beauty of XAML based applications is that you can put whatever media you want inside objects, so if you want to fill in Ellipse or a Path with an image you can. The easiest way to do it is just declaratively, in XAML:

 

<Rectangle x:Name="mainPic" Width="448" Height="386" Canvas.Left="288">

      <Rectangle.Fill>

            <ImageBrush ImageSource="pic.jpg" />

      </Rectangle.Fill>

</Rectangle>

 

 

This is great for static content, but what if you want to set it programmatically using Downloader object.

 

Below is the solution for 1.0 (I tested in on Feb CTP release)

 

var wpfe;

 

function root_Loaded(sender, args) {

      wpfe = document.getElementById("wpfeControl1");

     

      mainDownloader = wpfe.createObject("Downloader");

      mainDownloader.completed = "javascript:mainDownloadCompleted";

      mainDownloader.DownloadProgressChanged = "javascript:mainDownloadProgressChanged";

     

      mainDownloader.open("GET", "SomePic.JPG", true);

      mainDownloader.send();

     

}    

 

function mainDownloadCompleted(sender, args) {

    sender.findName("mainPic").Fill.ImageSource = sender.Uri;

}

 

 

Apart from setting the downloader, the most important code line is this:

 

    sender.findName("mainPic").Fill.ImageSource = sender.Uri;

 

When the image gets downloaded we set the Rectangle’s ImageBrush to its URL.

 

Now solution for 1.1 in C#:

 

public void Page_Loaded(object o, EventArgs e)

        {

            // Required to initialize variables

            InitializeComponent();

 

            Downloader downloader = new Downloader();

            downloader.Completed += new EventHandler(downloader_Completed);

            downloader.Open("GET", new Uri("SomePic.JPG", UriKind.Relative), true);

            downloader.Send();

        }

 

 

void downloader_Completed(object sender, EventArgs e)

        {

           (mainPic.Fill as ImageBrush).ImageSource = (sender as Downloader).Uri;

        }

 

Once again, the most important code line is this

 

(mainPic.Fill as ImageBrush).ImageSource = (sender as Downloader).Uri;

 

Notice, how much casting we have to do, because C #is a strongly typed language.

 

Hope this helps.

Friday, May 04, 2007 3:15:45 PM (GMT Standard Time, UTC+00:00)  #    Comments [0] -
Silverlight

This is a quick post describing my first impressions with Silverlight 1.1 and Orcas Beta 1.

 

Orcas:

·        Overall: a step forward in a good direction. I really enjoy working with it. Can’t wait for Resharper to come out for it J.

·        I love that I can finally debug JavaScript.

·        Nice and clean separation: When you have Page.html, you also get Paga.html.js. Now, we have code behind for everythingJ.

·        XAML explorer is a lot faster when I compare it to VS 2005.

·        Running Silverlight application launches default browser in the system (in my case Opera). There is no plugin for Opera to make Silverlight working, so I always get the lovely image Get Silverlight. I had to set IE to be my default browser. In VS 2005 IE launched always no matter what was the default browser, I preferred it that way.

·        Ctrl + Right Arrow (and Ctrl + Left Arrow) don’t work! When I checked keyboard shortcuts in 2005 and Orcas they said exactly the same thing: Edit.MoveControlRight. Apparently this doesn’t work in Orcas on my machine. BTW: I had no idea how much I use this feature until it stopped working.

 

 

 

Silverlight:

  • Between 1.0 and 1.1 a HUGE step forward. I mean CLR on MAC, DLR and so on. My head still hurts….
  • It’s amazing I can write C# in my client app. I’ve written an Image Gallery for Silverlight 1.0 (actually when I wrote it, it was still WPF/E) and it wasn’t a joy ride. I didn’t enjoy debugging JavaScript with alert.
  • I’m rewriting it now to learn 1.1 and I’ve noticed that productivity boost is amazing. I truly believe that it will be possible to write simply amazing Rich Internet Applications with this technology.

 

 

Small Tip when building Silverlight applications with Orcas:

 

When you’re adding objects to XAML code that you want to reference in the code behind, make sure you use prefix x before Name property:

 

<Rectangle x:Name="mainPic" Width="448" Height="386" />

 

x of course corresponds to

 

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

 

Without that little x you will get compilation errors. It is a little bit counter intuitive since when you press n you also get a property Name, but this Name corresponds to the default schema

 

 

 

Friday, May 04, 2007 2:41:01 PM (GMT Standard Time, UTC+00:00)  #    Comments [0] -
Orcas | Silverlight
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2008
Marcin Waligora
Sign In
All Content © 2008, Marcin Waligora
DasBlog theme 'Business' created by Christoph De Baene (delarou)