Silverlight minimal examples
I won’t be repeating the very basics of how to get started with silverlight here but will instead refer you to John Lam’s excellent blog post on the subject. I do have to warn you for this series we won’t be using the silverlight.rb created by the dsl template generator. I chose to do it this way so that you would better understand what is going on in the silverlight.rb file. I’ll assume from now on that you’re familiar with John’s post and move on.
You can see the end result of these 2 examples in action:
http://flanders.co.nz/silverlight/minimal/index.html
http://flanders.co.nz/silverlight/minimal2/index.html
To run a silverlight website you don’t need a server side configuration. I’m hosting those examples on a linux box without mono installed. It’s just serving up files.
The first thing we need to do is create a skeleton for our application.
I did this by opening up Powershell (or the command prompt) and navigating to c:\projects\silverlight and executing the command dsl minimal_ir_project
Now that that’s done we’re going to delete silverlight.rb from the app folder. We can do that by going into that folder ( cd minimal_ir_project\app ) and executing del silverlight.rb Next we’re going to navigate one level up ( cd .. ) and start the webserver and default browser by executing chiron /b
At this point we’re entirely ready to start developing
Let’s fire up an editor. For these examples I used notepad++ because Ruby In Steel has been acting up and it seemed a bit overkill to start up visual studio for the amount of coding we need to do in these 2 examples.
I first opened up the app.xaml file and gave it the following content.
This xaml ensures us that we will have a canvas container object and a TextBlock with name textblock. We’ve also set the font size of the text to 30 and if we don’t change the text through ruby it will read Hello world from XAML. To be able to show this xaml we need to edit the app.rb file and give it the following content:
include System::Windows
include System::Windows::Controls
Application.current.load_root_visual Canvas.new, “app.xaml”
The first 2 lines in that file are just like using statements in C# and provide access to the types living in those namespaces without having to fully qualify them. So instead of having to write System::Windows::Controls::Canvas you can use Canvas. And the next line tells our silverlight application that the visual root control has to be loaded as a Canvas type from the file app.xaml
If you switch now to your browser and click on the index.html file you will see Hello world from XAML in your browser. Very impressive, feel free to say woohoo
Let’s take it a little further and change the text of that text block to Hello world from IronRuby because after all it’s all about IronRuby. To do that change the content from app.rb to:
And the end result looks like this in Internet Explorer (Firefox 3 crashes with silverlight currently)
I think this would be the least amount of code you have to write to get going with silverlight development.
Let’s look at how that code would look when we don’t use xaml.
Let’s create a second project minimal_ir_project2 like before but now we delete both silverlight.rb and app.xml. The only file that is left now is app.rb Again you can start the webserver.
The content of app.rb should look like this:
This ruby code has the same effect as the previous app.xaml. We first create a canvas object, next we create a TextBlock object and configure it’s properties. After that we add the textblock objects to the children collection of the canvas instance. And lastly we set the canvas object to be the root visual of our silverlight application. And all that hard work yields the following result:
This concludes the first article in our series on Silverlight and IronRuby. Today we covered the very basics of silverlight development.
Technorati Tags: ironruby,silverlight
del.icio.us Tags: ironruby,silverlight
You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
April 14th, 2008 at 4:34 am
[…] can read more about it here: http://rubydoes.net/2008/04/14/silverlight-minimal-examples/ Share and Enjoy: These icons link to social bookmarking sites where readers can share and […]
April 14th, 2008 at 5:57 am
[…] Silverlight Minimal Examples (Jim) […]
April 15th, 2008 at 3:34 am
[…] yesterdays post we covered the very basics of using IronRuby with Silverlight. Today we’re going to look at […]
April 15th, 2008 at 1:23 pm
[…] yesterdays post we covered the very basics of using IronRuby with Silverlight. Today we’re going to look at how […]
April 17th, 2008 at 9:47 pm
[…] I didn’t post the last 2 days, but we’re back with a new post. In my previous posts (1, 2) we didn’t use the files as they are generated by the little DSL script, this was chosen […]