OpenAppDotOrg | MoviesOnITunes | SilverlightCommunity | Ajuby | CodeMunch

Silverlight / IronRuby using controls

Excellent post.

via rubydoes.net by casualjim on 4/15/08


In yesterdays post we covered the very basics of using IronRuby with Silverlight. Today we’re going to look at how we can use some of the basic controls that have been released for silverlight. One of the cool things about the controls is that Microsoft released the source code for them. Along with this source code they also released a test framework for testing your own controls. I think that is pretty cool :)
Shawn Burke explains how to use the testing framework they built for silverlight controls. In his blog post he also points you to the correct location for downloading the bits you need.

The next thing that is really cool about using silverlight is that it’s so easy to change the appearance of a silverlight control by using skinning. Corrina Barber explains how to go about that and she also has 4 sets of skins for the standard controls available for download.
See online previews of each skin:

Are you still here? Great :) Let’s look at some simple examples of using the standard controls that come with the Silverlight SDK. We won’t look into skinning those controls yet but for now will just look at how to use some of them. Like yesterday we’re going to look at 2 examples one that uses predominantly xaml and one that uses mostly ruby.

You can view the completed examples online:

I got my Ruby In Steel to work again so in the code download I’ve included the Visual studio solution and project files.

Download the completed code

The first sample will be mostly using xaml. The first thing we need to do is create a silverlight application. Just like yesterday I’ve removed the silverlight.rb file, but unlike yesterday we are not done preparing our app yet. The controls we’re going to be using for our samples live in Microsoft.Silverlight.Controls.dll and Microsoft.Silverlight.Controls.Extended.dll. These dll’s can be found on my pc in C:\Program Files\Microsoft SDKs\Silverlight\v2.0\Libraries\Client.  Copy those controls to the app folder of your application.  We’re now one step away from completing the preparations. The last thing we need to do is tell our application it requires the assemblies that contain the controls. To do this we need to add them in an AppManifest.xaml file. To generate such an AppManifest.xaml file we can use chiron with the parameters /m and /d:app

silverlight_controls_appmanifest

The file that got generated contains references to Microsoft.Silverlight.dll, as well as for the DLR (Microsoft.Scripting.dll) and for IronRuby (IronRuby.dll and IronRuby.Library.dll). We need to add the assemblies for the controls in this file.

silverlight_controls_appmanifest_edited

We’re now ready to start developing an application. The xaml file for the first sample is a little bit too long to show here but feel free to download the code and have a look. Instead we’ll highlight the most important bits. For the xaml to work for the Button control we need to reference Microsoft.Silverlight.Controls and for the watermarked textbox we need Microsoft.Silverlight.Controls.Extended. We need to add reference to those in our app.xaml file by adding a namespace declaration of which the content looks like: clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls

silverlight_controls_references_in_xaml

We then added some controls to the app.xaml file as you can see in the download, but in mean time have a look at the live demo.  The ruby file that drives this xaml is very basic and only has 3 lines of code, all the code that is necessary to load the xaml file.

silverlight_controls_app_rb

Our next example will allow us to do something more useful like responding to a click event on a button. This demo is more ruby centric and uses a very small xaml file, without references to the assemblies containing the controls.

silverlight_controls2_xaml

Instead of adding the references in xaml we’re going to add them to our ruby file by using the ruby require statement.

silverlight_controls2_references_ruby

Yesterday we saw how we could use find_name to retrieve a control and manipulate it. Today I added the method missing implementation from the silverlight.rb file to the ruby file. This will give us the ability to write xaml.control_name instead of xaml.find_name(’control_name’)

silverlight_controls2_method_missing

The next part is adding some more controls to the canvas like a StackPanel that will contain a Button and a WatermarkedTextbox.   Tomorrow we’ll look at how we can pretty up that ruby code :) but for now we’re just going to use the standard api. We first create the instances of the controls and configure them. Next we add them to the panel and then we add the panel to the canvas.

silverlight_controls2_adding_controls

The last step for our mini application to actually respond to an action from the user is to handle the click event on the button. Ruby makes this as easy as assigning a block to the click attribute on the button instance.

silverlight_controls_handling_events

And that’s all for today. Tomorrow we’ll look at putting ruby to work for us and prettying up that api for adding elements to a silverlight control.

Technorati Tags: ,

del.icio.us Tags: ,

Reblogged with MessageDance using Google Reader

Leave a Reply


OpenApp Media Network OpenApp Media Network

Various trademarks held by their respective owners.

Privacy Statement | Terms Of Service