Telerik blogs

Being Silverlight developer means that we are also kind of web developers. From this point of view, we also have to think about the web traffic that our applications generate. We need to reduce it as much as possible. So basically we need to reduce the size of the application's output - the .xap file.

In this post I'll try to summarize the five ways to reduce the size of a xap that I've shared today with Sofia's Silverlight User Group.

Understanding the Xap

In order to reduce the size of a xap file, we should first be aware of its structure. Basically it can contain only 4 things:

  • AppManifest.xaml - xaml file that identifies the packaged assemblies and the application entry point
  • Application Assembly - the one that contains the Application class.
  • Library assemblies.
  • Resource files - like images and videos

So we can smaller its size by reducing the code and resources of our assemblies, load assemblies on demand (exclude them from the xap) or minify some of the compiled library assemblies.

Start with the Resources

You should carefully think about the resources (Images, Videos...) - how you use and manage them! You have 3 options to choose from - embed them into assembly, add them as a content in the xap or leave them on the server as web content. Note that the last option can reduce the xap significantly. You have to check their "Build Action" and choose the correct one:

  • Resource - The resource gets embedded into the assembly, so respectively in the xap.
  • Content - The resource gets embedded into the xap. 
  • None - The resource won't be embedded into the assembly nor the xap. But you can "Copy it to the Output Directory", so use it as web resource, thus Load it on Demand.
You can read more about Silverlight resource files here.

Application Library Caching

It allows you to exclude assemblies form the xap with just a single click option, mark them as external parts and make them ready to be cached by the browser. I'm not going to dive into Assembly Caching, since Evan made a great post about it. Just read it and consider using it.

Load assemblies on Demand

This trick is possible due to the Silverlight API, that allows us to load assemblies into the AppDomain. We need to download one using simple WebClient, load it and use Reflection to use its types and resources. Although this is tricky and kind of obsolete (thanks to the Prism), one can find it interesting and implement it on his/her own. You can read more about this approach.

Modularity with Prism

Prism is probably the best approach out there for Silvrelight application development. The architecture allows us to slice the application into Moduls. Each module can be easily configured to be Loaded On Demand | When Available and On the Background.  So the resulting application consist of a lot of xaps and some of them are delayed and downloaded during run-time, which can significantly reduce the initial download time and respectively enhance the overall user experience. Download the latest binaries and play with the modularity example projects.
Note that if a Module reference assembly that is loaded with the main module, you can set its CopyLocal = False attribute, which means that the assembly won't be included into the xap. But since it will be already loaded into the AppDomain, everything is ok.

Dead-code removing - Minification

Although the Prism give us so much flexibility and great features, it can't fix one problem that comes with using already compiled assemblies. The abstract problem is that it doesn't matter how much code or features you use from one assembly - you get it as a whole. This problem was addressed long time ago and the solution is named "Minification" (the process of removing all unnecessary characters from source code, without changing its functionality). This is very popular trick in the JavaScript world (and it obviously makes sense - to reduce the size of a web page, by removing unused JS code). In the Telerik's Silverlight world the solution is called Assembly Minifier. It reduces the compiled assemblies size by removing the controls (code and resources) that your application doesn't need during run-time. The result is smaller assemblies, smaller xap, which results in faster download time and faster application loading. You can read my post for more information on how to use the tool.

Some more Tricks

  • Re-zip your xap package using higher compression ratio. Read the full story by Valeri Hristov.
  • Configure long expiration date on your IIS, so the xap will be cached by the user's browser, thus downloaded only once.
  • Create nice looking and professional SplashScreen that will reduce the bad experience from downloading big xap package.

Thank you for your time and keep in mind that the Xap size matters!

About the Author

Miroslav Miroslavov

is XAML enthusiast. You can follow him on Twitter at @mmiroslavov.



Comments are disabled in preview mode.