An IronPython Silverlight ApplicationThe Structure of a Dynamic Application![]() Silverlight & IronPython
Structure of a Silverlight ApplicationA basic Silverlight application consists of the following parts:
This is a great improvement on the Silverlight 1.1 packaging model that required four or five files for a simple application. In Silverlight 2 we can create a dynamic application with just a web page and a 'xap' file. Creating these 'xap' files is made simple with the use of Chiron. Chiron is a tool for packaging dynamic applications, and it can also serve applications from the filesystem whilst you are developing them. Chiron is part of the 'Dynamic Silverlight SDK' which you can get from: This SDK doesn't include the Silverlight documentation (the chm file), which you have to download separately. If you're in a hurry, you can skip this page and just use the simple examples from the next article: If you want to understand what you're working with, then read on... Embedding the Silverlight ControlEmbedding a Silverlight control in a webpage is straightforward. Here is a typical example that displays the 'Get Microsoft Silverlight' link and image if Silverlight is not installed.
<div id="silverlightControlHost" width="480" height="320">
<object data="data:application/x-silverlight,"
type="application/x-silverlight-2-b1"
width="480" height="320">
<param name="source" value="app.xap"/>
<param name="onerror" value="onSilverlightError" />
<param name="background" value="#00000000" />
<param name="initParams" value="reportErrors=errorLocation" />
<param name="windowless" value="true" />
<a href="http://go.microsoft.com/fwlink/?LinkID=108182" style="text-decoration: none;">
<img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft
Silverlight" style="border-style: none"/>
</a>
</object>
<iframe style='visibility:hidden;height:0;width:0;border:0px'></iframe>
</div>
You can also do this 'setting up' via Javascript (the old way with Silverlight 1.1) which gives you a finer degree of control. The 'onerror' parameter is Javascript functions to be called in the event of error. We can include some Javascript and CSS in the webpage to make the formatting of error messages prettier:
<!--
Error handling for when DLR errors are disabled (with
reportErrors=false, or not defined at all)
-->
<script type="text/javascript">
function onSilverlightError(sender, args) {
if (args.errorType == "InitializeError") {
var errorDiv = document.getElementById("errorLocation");
if (errorDiv != null)
errorDiv.innerHTML = args.errorType + "- " + args.errorMessage;
}
}
</script>
</head>
<body>
<!--
Syntax/Runtime errors from Silverlight will be displayed here.
-->
<div id='errorLocation' style="font-size: small;color: Gray;"></div>
The 'xap' FileThe 'xap' file is really just a zipfile which you can construct manually or can be autogenerated for you by Chiron. The command line magic to make Chiron create a 'xap' file from a directory is: Chiron /d:dir_name /z:app_name.xap If you are running this on the Mac (requires OS X and Mono), then the command line will look something like this: mono bin/Chiron.exe /d:dir_name /z:app_name.xap More importantly, you can use Chiron to test your application from the filesystem, without having to create a 'xap' file. If your application is called 'app.xap' then your application files should be kept in a directory named 'app'. Chiron will act as a local server, and dynamically create the 'xap' file when the browser requests it. The command to launch Chiron as a webserver is: Chiron /w By default this serves on localhost port 2060. The XAML Manifest FileThe way that Silverlight knows an application is dynamic is through the manifest file. This is an XML (actually XAML) file contained in the 'xap' file. If you aren't using any assemblies beyond IronPython (or IronRuby) then you can just use the following, or let Chiron generate the manifest file for you:
<Deployment
xmlns="http://schemas.microsoft.com/client/2007/deployment"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
RuntimeVersion="2.0.30226.00"
EntryPointAssembly="Microsoft.Scripting.Silverlight"
EntryPointType="Microsoft.Scripting.Silverlight.DynamicApplication">
<Deployment.Parts>
<!-- Add additional assemblies here -->
<AssemblyPart Source="Microsoft.Scripting.dll" />
<AssemblyPart Source="IronPython.dll" />
<AssemblyPart Source="IronPython.Modules.dll" />
<AssemblyPart Name="Microsoft.Scripting.Silverlight"
Source="Microsoft.Scripting.Silverlight.dll" />
</Deployment.Parts>
</Deployment>
XML manifest files makes Silverlight applications fit for the enterprise... The dlls listed there are part of the Dynamic Silverlight SDK, and will be included in your manifest file automatically by Chiron. It is interesting to note the 'EntryPoint' attributes in the 'Deployment' element. This is how Silverlight knows how to execute a dynamic application - and in fact a dynamic application is just a normal C# application as far as Silverlight is concerned. It is the Microsoft.Scripting.Silverlight that does the magic for us. To use additional assemblies in your applications, beyond the standard ones that are part of Silverlight, they need to be included in the manifest. The fancy new controls for Silverlight 2 are actually provided as external assemblies (with sources and tests!). Your Python ApplicationIf your Silverlight app is written in IronPython, then you will need to include at least one Python file (plus any you import of course) in your application. For Silverlight to find it, the main script must be called app.py (or app.rb for IronRuby or app.js for managed JScript). The next article looks at how to create the Python file: 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.
Last edited Sun Jun 1 23:38:15 2008. Counter... |
|
|
Blogads
Follow me on: Tech Jobs |