ePlaice / For the Best Software on the Net

Mainly Free and Open Source Software


WPF Navigation

SVG to XAML Conversion | Standalone WPF Applications | Silverlight 3 | Silverlight 3 Project Template | Silverlight Libraries | XAML Introduction | WPF Data Binding | WPF Styles and Templates |

Valid XHTML 1.1

Latest news

10 Jul 2009: Silverlight Version 3 released

22 Aug 2008: SharpDevelop 3.0 Beta 2 has been released - you can get it on Sourceforge as well as at Sharpdevelop downloads.

Links:

A Project Template for Silverlight 3 Using SharpDevelop

I found a way to use SharpDevelop to create Silverlight 1.1 dll's, so I was fairly confident that I could do something similar with Silverlight 2. Although the release of Silverlight 2 has caused a small change in the way the applications are built and installed, I am pleased to say that SharpDevelop 3.0 Beta 2 is in my view still a viable way forward. It's a little disappointing that there is no Sharpdevelop built in support for Silverlight, but I guess this is down to heavy workload and Silverlight not being a project priority. If you want to see the logo above (which I built using my template to call the excellent SilverlightFX library) you will need to have Silverlight 2 installed. Try clicking on it to see the Flip effect.

So in the meantime I have devised my own method of compiling Silverlight using the SharpDevelop environment involving creating a Silverlight template. At this stage I should emphasise that if you pick up the source of a Silverlight application built using Visual Studio, you should find that SharpDevelop will effortlessly compile the application and create the .xap file. The only slight hiccup is that an asp.net application will not compile straight off, however there is a workaround for this, which I will detail later. So my Silverlight template is only of real use if you want to build your own Silverlight application from scratch. In this case using my template for SharpDevelop will create you a basic solution just as effortlessly as any other of the inbuilt templates such as WPF. Recently, I have found a way to get the correct Import statement in the project file, so once you have set up the template and ancillary files fully detailed here future project builds will be fully automated. Just one slight problem, which I hope to have fixed shortly, is that you get two System.Core assemblies referenced. This seems to be a bit bizarre, but nevertheless it is easy to delete the one without the public key token from within the IDE - on the other hand it does not seem to create any problems by just leaving it alone.

Creating Silverlight 3 XAPs

If you are writing a managed code application, as for example using C#, you can package up all the installation files into a XAP file. This is in fact just a zip file that contains XAML manifest file and all the dlls that are required for the on-line control. You can use something like 7-Zip to view the contents of the file if you are curious as to what it contains. This is actually a bit of a simplification when deploying your Silverlight application because it means you can package the installation into one zipped file, saving space and creating simplicity. All your html code has to do is deal with error handling and calling the XAP file. What's more there is no need for separate javascript files, such as Silverlight.js and files such as Testpage.html.js which is a relief since AVG did not like these double subscripted .js files.

SharpDevelop Silverlight Template

This is my latest attempt and now creates the correct Import file statement, which means that setting up a basic Silverlight project solution can now be fully automated using SharpDevelop so that it creates all the project files using the input project name. I called it SilverlightApplication.xpt and placed it in the SharpDevelop templates directory i.e. 'C:\Program Files\SharpDevelop\3.0\data\templates\project\CSharp'. When you start SharpDevelop and ask to create a New Project you should see 'Silverlight 2.0 Application' in the Templates box for Category C# and NET 3.0.

<Template originator = "David Herring" created = "03/12/2008">
    
    <!-- Template Header -->
    <TemplateConfiguration>
        <Name>Silverlight 3.0 Application</Name>
        <Category>C#</Category>
        <Subcategory>.NET 3.0</Subcategory>
        <Icon>C#.Project.Form</Icon>
        <Description>Creates a Simple Silverlight 3.0 Application</Description>
        <SupportedTargetFrameworks>v3.0</SupportedTargetFrameworks>
    </TemplateConfiguration>
        
    <!-- Actions -->
    <Actions>
        <Open filename = "Page.xaml"/>
    </Actions>
    
    <Project language = "C#">
        <Imports clear="True">
            <Import Project="$(MSBuildExtensionsPath)\Microsoft\Silverlight\v3.0\Microsoft.Silverlight.CSharp.targets" />  
        </Imports>    
        <CreateActions>
            <RunCommand path = "/SharpDevelop/BackendBindings/TemplateCommands/AddDotNet35ReferencesIfTargetFrameworkIs35"/>
        </CreateActions> 
        
        <ProjectItems>
            <Reference Include="System.Windows" />
            <Reference Include="mscorlib" />
            <Reference Include="system" />
            <Reference Include="System.Core" />
            <Reference Include="System.Xml" />
            <Reference Include="System.Windows.Browser" />
        </ProjectItems>
                    
        <PropertyGroup>
            <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
            <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
            <ProductVersion>9.0.21022</ProductVersion>
            <SchemaVersion>2.0</SchemaVersion>
            <ProjectGuid>{CD3626B6-0F1E-4B77-A27B-2F080CAEF4BA}</ProjectGuid>
            <ProjectTypeGuids>{A1591282-1198-4647-A2B1-27E5FF5F6F3B};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>
            <OutputType>Library</OutputType>
            <AppDesignerFolder>Properties</AppDesignerFolder>
            <RootNamespace>${StandardNamespace}</RootNamespace>
            <AssemblyName>${StandardNamespace}</AssemblyName>
            <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
            <NoStdLib>true</NoStdLib>
            <SilverlightApplication>true</SilverlightApplication>
            <XapOutputs>true</XapOutputs>
            <GenerateSilverlightManifest>true</GenerateSilverlightManifest>
            <XapFilename>${StandardNamespace}.xap</XapFilename>
            <SilverlightManifestTemplate>Properties\AppManifest.xml</SilverlightManifestTemplate>
            <SilverlightAppEntry>${StandardNamespace}.App</SilverlightAppEntry>
            <TestPageFileName>TestPage.html</TestPageFileName>
            <CreateTestPage>true</CreateTestPage>
        </PropertyGroup>
        
        <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
            <NoStdLib>true</NoStdLib>
            <OutputPath>ClientBin\</OutputPath>
        </PropertyGroup>
        <Files>
            <File name="App.xaml" buildAction="Page" Generator="MSBuild:CompileXaml"  
    language="XML"><![CDATA[<Application x:Class="${StandardNamespace}.App"
    xmlns="http://schemas.microsoft.com/client/2007"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    >
    <Application.Resources>
         
    </Application.Resources>
</Application>]]></File>
            <File name="App.xaml.cs" SubType="Code" DependentUpon="App.xaml"><![CDATA[using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
 
namespace ${StandardNamespace}
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
 
        public App()
        {
            this.Startup += this.Application_Startup;
            this.Exit += this.Application_Exit;
            this.UnhandledException += this.Application_UnhandledException;
 
            InitializeComponent();
        }
 
        private void Application_Startup(object sender, StartupEventArgs e)
        {
            // Load the main control
            this.RootVisual = new Page();
        }
 
        private void Application_Exit(object sender, EventArgs e)
        {
 
        }
        private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
        {
 
        }
    }
}]]></File>
            <File name="Page.xaml" buildAction="Page" Generator="MSBuild:CompileXaml" 
      language="XML"><![CDATA[<UserControl x:Class="${StandardNamespace}.Page"
    xmlns="http://schemas.microsoft.com/client/2007" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    >
    <Grid>
        
    </Grid>
</UserControl>]]></File>
            <File name="Page.xaml.cs" SubType="Code" DependentUpon="Page.xaml"><![CDATA[using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
 
namespace ${StandardNamespace}
{
    /// <summary>
    /// Interaction logic for Page.xaml
    /// </summary>
    public partial class Page : UserControl
    {
        public Page()
        {
            InitializeComponent();
        }
    }
}]]></File>
            <File name="Properties/AssemblyInfo.cs" src="SilverAssemblyInfo.cs" />
            <File name="Properties/AppManifest.xml" src="SilverlightManifest.xml" />
        </Files>
    </Project>
</Template>  

The template automatically creates code to generate the xap file and the TestPage.html file

Include an AppManifest.xml File

The template automatically adds the manifest file to the Properties folder and includes it in the Project. The file is called SilverlightManifest.xml and placed in the template directory. It looks like this :-

<Deployment xmlns="http://schemas.microsoft.com/client/2007/deployment"
        xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"
>
    
<Deployment.Parts>
    
</Deployment.Parts>
</Deployment>  

Include an AssembyInfo.cs File

Unfortunately the WPF AssemblyInfo template does not work in Silverlight so I created a new one and called it SilverAssemblyInfo.cs and placed it in the template directory. Here is the code :-

using System.Reflection;
using 
System.Runtime.CompilerServices;
using 
System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following 
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("${ProjectName}")]
[assembly: AssemblyDescription(
"")]
[assembly: AssemblyConfiguration(
"")]
[assembly: AssemblyCompany(
"")]
[assembly: AssemblyProduct(
"${ProjectName}")]
[assembly: AssemblyCopyright(
"")]
[assembly: AssemblyTrademark(
"")]
[assembly: AssemblyCulture(
"")]

// Setting ComVisible to false makes the types in this assembly not visible 
// to COM components.  If you need to access a type in this assembly from 
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("1a46ec60-9165-4375-b8fd-cc335afe5bc2")]

// Version information for an assembly consists of the following four values:
//
//      Major Version
//      Minor Version 
//      Build Number
//      Revision
//
// You can specify all the values or you can default the Revision and Build Numbers 
// by using the '*' as shown below:
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion(
"1.0.0.0")]

App.xaml and App.xaml.cs Files

The template automatically generates an App.xaml and App.xaml.cs file within the project (as well as Page.xaml and Page.xaml.cs).
Here is an example of the generated App.xaml file :-

<Application xmlns="http://schemas.microsoft.com/client/2007"
             xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml" 
             x:Class
="MySLTest.App"
             
>
    
<Application.Resources>
        
    
</Application.Resources>
</Application>

Here is an example of the generated App.xaml.cs file :-

using System;
using 
System.Collections.Generic;
using 
System.Linq;
using 
System.Windows;
using 
System.Windows.Controls;
using 
System.Windows.Documents;
using 
System.Windows.Input;
using 
System.Windows.Media;
using 
System.Windows.Media.Animation;
using 
System.Windows.Shapes;

namespace 
MySLTest
{
    
public partial class App : Application
    {

        
public App()
        {
            
this.Startup += this.Application_Startup;
            this
.Exit += this.Application_Exit;
            this
.UnhandledException += this.Application_UnhandledException;

            
InitializeComponent();
        
}

        
private void Application_Startup(object sender, StartupEventArgs e)
        {
            
// Load the main control
            
this.RootVisual = new Page();
        
}

        
private void Application_Exit(object sender, EventArgs e)
        {

        }
        
private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
        {

        }
    }
}

Using the Template

Once you have placed the template in the Sharpdevelop folder you can create a new Silverlight project in much the same way as you do a normal WPF project. You just specify on the SharpDevelop GUI the name of the project and where it will be placed. If you compile you should get a TestPage.html file in the ClientBin which when run just displays a blank page. You then have to start the creative bit of adding your code to the generated project.

Basic Silverlight Application

If you use the template above incorporating all the changes detailed you should be in a position to build a Silverlight control without the need for using Microsoft Visual Studio at all. Although the compilation process should produce a XAP file you will find that running this just produces a blank screen. Most of the tutorials I have seen assume that you are working with Visual Studio and Expression Blend. I am not going to pretend it's any easier just using SharpDevelop but it should save a bit of investment in expensive development tools.

Using Kaxaml

With the latest beta version 1.5 of Kaxaml there is now some limited functionality in the editor to view and edit Silverlight XAML files. This is very useful and actually uses the SharpDevelop editor. It seems a pity that the two developments cannot get together so that all of this functionality can be found in the one IDE. Also, this version of Kaxaml provides some support for Silverlight, with more promised in the future. Unfortunately, this does not seem to be much of a priority for SharpDevelop at the moment, I suspect mainly because the developers are spending all their efforts on the full release of SharpDevelop 3.0.

In SharpDevelop Hit the Build Solution Button

If all is well you should find your XAP file (e.g. MySLTest.xap) and all the constituent files that make up this compressed file in the ClientBin folder.

Test it Out

As part of the build you also get a TestPage.html file which allows you to check the workings. For deployment all you need is the XAP file and an html wrapper along the lines of TestPage.html plus maybe any resource files such as image files used by your application.