IronPython with Silverlight

Cumartesi, 17 Ekim 2009 18:24 by ikivanc
Silverlight + IronPython = <3

Silverlight projects can be directly developed by 2 main languages; C# and Visual Basic. After Silverlight 1.1 release Silverlight now support Dynamic Languages. Now we can develop applications on IronPython!

After this support Silverlight can be developed by IronPython and IronRuby which runs on Dynamic Language Runtime (DLR). 

With “The Silverlight Dynamic Languages” we can develop apps on .NET with .NET libraries.
 
For Dynamic Silverlight Development Kit come with us with their releases. IronRuby also contains IronPython binaries for using them together. Unless you need a specific version of IronPython, just download IronRuby's latest release to get both.

To get lastest release of IronPython:  http://ironpython.codeplex.com 

There is a tool called Chiron for developing Silverlight apps. 

Developing Silverlight apps with Dynamic Languages is a bit harder than C# apps. We couldn’t edit IronPython Project code directly Blend or we couldn’t develop it with desing preview. Now we can only run and edit IronPython Silverlight projects in visual studio.

As you know Silverlight project works with XAP package files in the browser.  XAP file contains files which a Silverlight application requires files for working in a browser. DLLs of Silverlight project code and XAML.  We will use Chiron for generate XAP file for Dynamic Language Silverlight Application and we will use it for serving our Silverlight project.

What is Chiron?
Chiron is a Dynamic Languages Silverlight development tool for languages runs on DLR(Dynamic Language Runtime). It helps us for developing IronPython with Silverlight. XAML code and PY files.

Chiron generates a XAP file and uses it with a server. Server runs project at Localhost.

Chiron is an executable file and we can use it from “cmd”. We can access Chiron like below.
 
chiron_cmd 

Let’s check out a standard IronPython-Silverlight project. Which files are included in our Project template: 

Projem\
Projem \index.html
Projem \app\
Projem \app\app.py
Projem\app\app.xaml
Projem\ assets\


app.xaml 
<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 Height="42" FontSize="28" Margin="36,40,170,0" VerticalAlignment="Top" x:Name="metin">Wooow, Supeeer!</TextBlock>
<Button Height="38" HorizontalAlignment="Left" Margin="27,103,0,0" VerticalAlignment="Top" Width="112" Content="Dugme" x:Name="dugme"/>
</Canvas>

app.py
from System.Windows import Application
from System.Windows.Controls import Canvas
 
xaml = Application.Current.LoadRootVisual(Canvas(), "app.xaml")
xaml.metin.Text = 'Wooow, Supeeer!'
xaml.dugme.Content = 'Dugme'    #Dugme means Button in English


index.html 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
 
<head>
    <title>SilverlightApplication</title>
    <style type="text/css">
    html, body {
        height: 100%;
        overflow: auto;
    }
    body {
        padding: 0;
        margin: 0;
    }
    #silverlightControlHost {
        height: 100%;
        text-align:center;
    }
    </style>
    <script type="text/javascript" src="Silverlight.js"></script>
    <script type="text/javascript">
        function onSilverlightError(sender, args) {
            var appSource = "";
            if (sender != null && sender != 0) {
              appSource = sender.getHost().Source;
            }
 
            var errorType = args.ErrorType;
            var iErrorCode = args.ErrorCode;
 
            if (errorType == "ImageError" || errorType == "MediaError") {
              return;
            }
 
            var errMsg = "Unhandled Error in Silverlight Application " +  appSource + "\n" ;
 
            errMsg += "Code: "+ iErrorCode + "    \n";
            errMsg += "Category: " + errorType + "       \n";
            errMsg += "Message: " + args.ErrorMessage + "     \n";
 
            if (errorType == "ParserError") {
                errMsg += "File: " + args.xamlFile + "     \n";
                errMsg += "Line: " + args.lineNumber + "     \n";
                errMsg += "Position: " + args.charPosition + "     \n";
            }
            else if (errorType == "RuntimeError") {           
                if (args.lineNumber != 0) {
                    errMsg += "Line: " + args.lineNumber + "     \n";
                    errMsg += "Position: " +  args.charPosition + "     \n";
                }
                errMsg += "MethodName: " + args.methodName + "     \n";
            }
 
            throw new Error(errMsg);
        }
    </script>
</head>
<body>
    <form id="form1" runat="server" style="height:100%">
    <div id="silverlightControlHost">
        <object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">
          <param name="source" value="ClientBin/SilverlightApplication.xap"/>
          <param name="onError" value="onSilverlightError" />
          <param name="background" value="white" />
          <param name="minRuntimeVersion" value="3.0.40624.0" />
          <param name="autoUpgrade" value="true" />
          <a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40624.0" style="text-decoration:none">
               <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style:none"/>
          </a>
        </object><iframe id="_sl_historyFrame" style="visibility:hidden;height:0px;width:0px;border:0px"></iframe></div>
    </form>
</body>
</html>


Let’s generate XAP Package. Chiron generates XAP file in our project folder.

>Chiron.exe /directory:Projem\app /zipdlr:app.xap 

 cmd_xap 
Files in Silverlight Dynamic Language SDK must be at the same directory with our project.

It will add some dll files to XAP file which are necessary for Ipy with Silverlight.

Check out what a XAP file contains. As you know in after Running our C# Silverlight project in Visual studio, our xaml and cs files are complied to a DLL assembly file. Bu in Dynamic Languages it doesn’t complied. XAP file contains them with their original format as Py and xaml files.

 xap_icerik 
After that on our project we will follow this steps: 
Run Chiron as a web server:

> cd Projem
> Chiron.exe /webserver 
chiron_server 

Chiron works as a web server on port localhost:2060 and will open root.
 
server_localhost

We can run our app now by the clicking index.html
 
IronPython_browser

But developing apps in Silverlight with IronPython is a bit harder then developing with C# in Blend.
You can generate XAML code via Blend design tool. I use C# Silverlight Project desing tools in Blend to generate XAML and I code IronPython code with manually.

PDF version of this article >> 6 - IronPython with Silverlight

If you have any questions or discover any errors / typos please let me know ik@ibrahimkivanc.com

All The Best!

Yorum ekle


 

  Country flag

biuquote
  • Yorum
  • Canlı önizleme
Loading