Using the PTVS for a Silverlight Project

My book, “Professional IronPython” doesn’t discuss how to work with technologies such as Silverlight, so I thought I’d see how hard it would be given that PTVS comes with support for a Silverlight project. This post adds to my previous post about PTVS. Of course, you’ll need to have PTVS installed on your system before you begin working with this example.

To begin this example, you start Visual Studio as normal and choose File | New to create a new project. The New Project dialog box should contain an entry for an IronPython Silverlight Web Page template as shown here. If you don’t see this template, then you probably don’t have IronPython 2.7 installed on your system and can use the instructions found in the IronPython 2.7 and PTVS blog entry to install it.

Silverlight01

Give your application a name (the example uses the default of SilverlightPage1) and click OK. You’ll see a new project appear like the one shown here:

Silverlight02

What I find interesting is that there is a designer provided for this template, but none provided for the Windows Forms application. I tried the Designer out and it works well. There isn’t any difference in creating a Silverlight application using this template than any other language I’ve tried. However, the list of controls you can access is limited. Here’s what the default Toolbox entries look like:

Silverlight03

I tried adding a control to the list, just to see what would happen. When you open the Choose Toolbox Items dialog box, you’ll see that all of the normal controls are already selectedthey simply don’t appear in the Toolbox. I even tried checking a few controls on the Silverlight-specific tab without any result. So, my thought is that you’re limited to the controls show. If you know otherwise, please contact me at John@JohnMuellerBooks.com. Even so, you can create a business application without any problem using the controls provided.

The Silverlight front end for your project uses HTML-like statements, as you might expect. You create linkage between the Web page and your IronPython code behind just as you would any other language. Here’s what the default sample code looks like:

<script type="text/python" src="SilverlightPage1.py"></script>

The code behind looks just like any other IronPython code you might have created in the past. Here’s the default example code:

def SayHello(s,e):
    window.Alert("Hello, World!")
  
document.Button1.events.onclick += SayHello

In short, you don’t have to worry about any odd programming techniques to make this example work. I was frankly surprised at just how nicely things do work with this template. There are a few oddities you’ll encounter. The first is that when you press F5 to start debugging your application, the IDE starts the Chiron development server, rather than the default. That’s not too odd, but it’s something you should know about.

The second is that the application doesn’t automatically appear in your browser. So, you’re waiting for something to happen and nothing does. Instead, you must right click the server entry in the Notification Area and choose Open in Web Browser from the context menu. At this point, I received a directory listing like the one shown here, rather than the application I expected.

Silverlight04

Clicking SilverlightPage1.html did display the application. In fact, it displayed a lot more. I also received an IronPython Console I could use for debugging tasks, as shown here:

Silverlight05

As shown in the figure, you can minimize the IronPython Console when not needed, so you can see your application better. Overall, I was very impressed with the Silverlight template offering and think it’s a great addition to the IronPython developer’s toolbox. Be sure to look for additional PTVS entries in my blog as time permits and to let me know your thoughts about it!