Getting Started with IronPython and Silverlight

Examples of Minimal IronPython Applications

Some of the Silverlight 2 controls

 

 

Minimal IronPython Example 1

This is just about the shortest IronPython file for the app.py in your dynamic application:

from System.Windows import Application
from System.Windows.Controls import Canvas

xaml = Application.Current.LoadRootVisual(Canvas(), "app.xaml")
xaml.textblock.Text = 'Hello world from IronPython'

Technically it's cheating of course, it's loading a xaml file from the 'xap'. The call to Application.Current.LoadRootVisual loads the xaml and makes it visible in the control. It also returns the object tree created, which we can manipulate programatically. Because the xaml (below) includes a textblock which has an x:Name attribute set, we can access the textbox as an attribute of the object tree.

<Canvas x:Class="System.Windows.Controls.Canvas"
    xmlns="http://schemas.microsoft.com/client/2007"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Name="parentCanvas">

<TextBlock x:Name="textblock" FontSize="30">Hello world from XAML</TextBlock>

</Canvas>

XAML defines a tree of objects that represent the user interface. You can use this to create elements of the interface include animations and 'skinning' (styling) UI elements.

Anything that can be done with XAML can be done from code and there is a direct relationship between XAML elements and their corresponding classes. For example, TextBlock XAML elements have a corresponding TextBlock class. Although I am generally no fan of visual design tools, nor of writing XML by hand, there are occassions when XAML is less verbose than the equivalent code. Dynamic languages are particularly good at manipulating test, and XAML is just text, so you can dynamically generate and consume XAML.

Minimal IronPython Example 2

This example uses no XAML at all:

from System.Windows import Application
from System.Windows.Controls import Canvas, TextBlock

canvas = Canvas()
textblock = TextBlock()
textblock.FontSize = 24
textblock.Text = 'This Really Works!!'
canvas.Children.Add(textblock)

Application.Current.RootVisual = canvas

Instead of loading XAML we directly assign to the RootVisual.

IronPython Controls Example 1

This example shows off some of the shiny new Silverlight 2 controls. The UI is created from XAML (which I borrowed from this sample).

I won't list all the XAML here, you can easily download it and take a peek.

<TextBlock Text="TextBlock" Margin="5"/>
<TextBox Text="TextBox" Margin="5" />
<WatermarkedTextBox x:Name="watermark" Margin="5"/>
<ContentControl Content="ContentControl" Margin="5"/>
<Button Content="Button w/ ToolTip" Margin="5" ToolTip="ToolTip"/>
<ToggleButton Content="ToggleButton" Margin="5"/>
<RadioButton Content="RadioButton" Margin="5"/>
<CheckBox Content="CheckBox" IsThreeState="True" Margin="5" />
<HyperlinkButton Content="HyperlinkButton"
 NavigateUri="http://blogs.msdn.com/kathykam" Margin="5"/>
<RepeatButton Content="RepeatButton" Margin="5"/>

The new controls come as add-on assemblies (for which you caan download the C# source and see how they are implemented). To use them you need to list them in the manifest file:

<AssemblyPart Name="System.Windows.Controls"
 Source="System.Windows.Controls.dll" />
<AssemblyPart Name="System.Windows.Controls.Extended"
 Source="System.Windows.Controls.Extended.dll" />

IronPython Controls Example 2

Using controls from code

Of course using the new controls from code is much more interesting. Before we can import the new controls we need to load the assemblies, which requires adding a reference using the full strong name and public key token of the assemblies. For the initial release of these controls, the strong names look like:

System.Windows.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
System.Windows.Controls.Extended, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35

This example creates a button and a 'watermarked textbox'. When the button is pushed, text in the textbox is put into a textblock above.

This code shows how easy to use from code the controls are.

import clr
clr.AddReference('System.Windows.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35')
clr.AddReference('System.Windows.Controls.Extended, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35')

from System.Windows import Application, Thickness
from System.Windows.Controls import (
    Button, Canvas, Orientation,
    StackPanel, WatermarkedTextBox
)

xaml = Application.Current.LoadRootVisual(Canvas(), "app.xaml")
textblock = xaml.FindName('textblock')

panel = StackPanel()
panel.Margin = Thickness(50)
panel.Orientation = Orientation.Horizontal


button = Button()
button.Content = 'Push Me'
button.FontSize = 18
button.Margin = Thickness(10)

waterbox = WatermarkedTextBox()
waterbox.FontSize = 18
waterbox.Margin = Thickness(10)
waterbox.Width = 200
waterbox.Watermark = 'Type Something Here'

def onClick(s, e):
    textblock.Text = waterbox.Text

button.Click += onClick

panel.Children.Add(button)
panel.Children.Add(waterbox)

xaml.Children.Add(panel)

Simple Animation Example

One of the great things you can do with Silverlight is create animations and visual effects. Creating simple animations can be a bit fiddly (several different classes involved), but the basics are straightforward.

This example animates the font size of some text when the mouse moves over it.

from System import TimeSpan
from System.Windows import Application, Duration
from System.Windows.Controls import Canvas, TextBlock
from System.Windows.Media.Animation import (
    DoubleAnimation, Storyboard
)

root = Canvas()

Application.Current.RootVisual = root

root.Children.Clear()
root.Resources.Clear()

t = TextBlock()
t.FontSize = 20
t.Text = 'Move the Mouse Over Me'
root.Children.Add(t)

sb = Storyboard()
duration = Duration(TimeSpan.FromSeconds(0.25))
a = DoubleAnimation()
a.Duration = duration
sb.Duration = duration
sb.AutoReverse = True
sb.Children.Add(a)

Storyboard.SetTarget(a, t)
Storyboard.SetTargetProperty(a, 'FontSize')
a.From = 20
a.To = 30

def anim(s, e):
    print 'Starting'
    sb.Begin()

t.MouseEnter += anim

root.Resources.Add(sb)

I particularly like the little dance we have to go through to set the duration. Being able to set a float directly would be waaaay too easy...

The next article explores some of the Silverlight APIs, using a web based ide to experiment:

For buying techie books, science fiction, computer hardware or the latest gadgets: visit The Voidspace Amazon Store. If you're looking for a new techie job, try the Voidspace Tech Job Board. This is part of the Hidden Network of technology and programming jobs.

Hosted by Webfaction

Return to Top

Page rendered with rest2web the Site Builder

Last edited Sat Mar 22 18:33:43 2008.


Counter...


Voidspace: Cyberpunk, Technology, Fiction and More
Search this Site:
 
Web Site

IronPython in ActionIronPython in Action

Blogads

Follow me on:

Twitter

Pownce

Jaiku

Del.icio.us

Shared Feeds

Tech Jobs

Hidden Network

Tech Jobs Board

Hosting for an agile web