Professional, Software

Hello Silverlight

I just wanted to try out what is the smallest Silverlight application without all the files that VS creates.

  1. I created a new project in VS (Chose “Generate HTML Test Page to host Silverlight…” option)
  2. Deleted following files
    1. AssemblyInfo.cs from Properties
    2. Only references I kept were (mscolib, System and System.Windows)
    3. Deleted App.xaml (& App.xaml.cs) as well as Page.xaml (& Page.xaml.cs)
  3. Created a new class named App (I kept the same name as what VS created, otherwise you will have to go into properties and change “Startup Object” drop down box from Silverlight Tab.
    1. this class had following code
   1: using System;
   2: using System.Windows;
   3: using System.Windows.Controls;
   4: 
   5: namespace SilverlightApplication2
   6: {
   7:     public class App : Application
   8:     {
   9:         public App()
  10:         {
  11:             Startup += this.Application_Startup;
  12:         }
  13: 
  14:         private void Application_Startup(object sender, StartupEventArgs e)
  15:         {
  16:             TextBlock hello = new TextBlock();
  17:             hello.Text = "Hello Silverlight";
  18:             RootVisual = new Canvas();
  19:             ((Canvas)RootVisual).Children.Add(hello);
  20:         }
  21:     }
  22: }

XAP that was created was 2449 Bytes.

Now lets see how this works..

To peek inside XAP file, I just renamed the XAP file to ZIP file.

It had two things…

  1. AppManifest.Xaml
  2. Silverlightapplication2.dll

AppManifest.xaml

One of the first thing to notice is that manifest (it is list of things that describes the application) is in XAML format and it looks like below.

<Deployment xmlns="http://schemas.microsoft.com/client/2007/deployment" 
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
            EntryPointAssembly="SilverlightApplication2" 
            EntryPointType="SilverlightApplication2.App" 
            RuntimeVersion="2.0.30226.2"> 
  <Deployment.Parts> 
    <AssemblyPart x:Name="SilverlightApplication2" Source="SilverlightApplication2.dll" /> 
  </Deployment.Parts> 
</Deployment>

  • Root tag (Deployment) has following pieces of interesting information
    • EntrypointAssembly: Tells the silverlight app activation code which assembly is the entrypoint for the assembly. This assembly is required to be in XAP
    • EntrypointType: This is the name of the application class that will be instantiated as part of the activation of an app
    • RuntimeVersion: version of silverlight this application was built against.
  • Deployment as collection of parts. These parts are pieces of application
    • My simple app only has one assembly so it has one AssemblyPart which points to SilverlightApplication2.dll.
  • When activation code instantiates the application class, constructor in my code hooks up the startup event for an application
  • when application is ready to execute, Startup event is fired. In the simple app code above, the only things I do…
    • Create a new TextBlock
    • Set a Text property of that textblock to be “Hello TextBlock”
    • Set a layout container (Canvas) as the root visual of the application
    • Add newly created Textblock on that canvas.

Hello Silverlight!

here is the project.

Advertisements
Standard

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s