Still looking for a sponsor Max Paulousky is looking for a Silverlight/.Net job in the Commonwealth

Share to Facebook Tweet this! Share to MySpace Share to Google Share to Live   Share via AddThis

XAPs Minifier. Avoiding Problem With Multiple Startup Objects

Abstract

As mentioned in my previous post, the add-on can generate error if it finds more than one Startup Objects in a solution. Usually, it happens when developers add new Xap file via adding new Silverlight project (File-New-Project-Silverlight Application). They delete App.xaml file but do not clear Startup Object in the project properties and it is not cleared automatically.

I decided to add additional check into the add-on to find out whether application has particular startup object or not. If the project does not have appropriate class, its startup object is just ignored.

Implementation

Following code checks whether project items items has a class with a given full name className:

private static bool HasCodeClass(ProjectItems items, string className)
{
  bool result = false;
  foreach (ProjectItem item in items)
  {
    if (item.ProjectItems != null && item.ProjectItems.Count > 0)
      result = HasCodeClass(item.ProjectItems, className);
    
    if (result)
      return result;
 
    if (item.FileCodeModel == null)
      continue;
 
    foreach (CodeElement element in item.FileCodeModel.CodeElements)
    {
      if (element.Kind == vsCMElement.vsCMElementClass && element.FullName == className)
        return true;
      else if (element.Kind == vsCMElement.vsCMElementNamespace)
      {
        CodeNamespace cnm = element as CodeNamespace;
        foreach (CodeElement elemenentN in cnm.Members)
        {
          if (elemenentN.Kind == vsCMElement.vsCMElementClass && elemenentN.FullName == className)
            return true;
        }
      }
    }
  }
  return false;
}

Initial parameter items is just an enumeration of all hi-level (1st level) items within a project.

Structure of levels of project items within a project

The algorithm is recursive one and it traverses all tree nodes. There are several types of nodes:

  • Nodes, that have own projects items (Folders, *.xaml etc: item.ProjectItems.Count > 0);
  • Nodes, that have not own projects items and do not contain classes (*.xml, *.config etc: item.FileCodeModel == null);
  • Nodes, that have not own projects items and contain classes (*.cs, *.vb etc).

The algorithm skip two first kinds of nodes. The third kind can have two options:

  • either it has just a class declaration (element.Kind == vsCMElement.vsCMElementClass )
  • or it has a namespace and a class declaration within the namespace (element.Kind == vsCMElement.vsCMElementNamespace).

Anyway, if a class has a required class name (elemenentN.FullName == className) than this project has correct startup object and can be the main silverlight project in the solution.

I doubt whether a solution may have more than one main project but if it has, a developer should divide it into two or even more solutions to have one main project per solution to be able to minify xap files.

Sources and Binaries

You can download sources here. The package can be downloaded here.

Add-on Update

Developers can go to Visual Studio Extension Manager (Main Menu-Tools-Extension Manager) to update the add-on. Just click Updates and the extension manager will start looking for a new version of Xaps Minifier.

Extension Manager Tool to update Xaps Minifier add-on

The screen above shows current and new version of the add-on and provide the Update button to get and install new version. After installing, the Visual Studio should be restarted to get the effect.

On the other hand, the package can be downloaded from my blog (check the Sources and Binaries section) and installed as described in the previous post.

Feedback

I need your feedback to improve the add-on. Please, feel free to contact me in any way (twitter, email max (dot) pau ##at## gmail (dot) com, comments on the blog) if you have any suggestion or idea.

Also, please advice this tool to your friends and colleagues who work with Silverlight.

Hope, this helps.

This work is licensed under a Creative Commons Attribution By license.

Leave a Comment [ RSS ]

  • re: XAPs Minifier. Avoiding Problem With Multiple Startup Objects

    Hi, thanks for great tool.

    Why you can't set that if tool find some projects with set startup app.xaml - it can give you a choise which of them need to use like startup project for minify xap packages?

    Really, at my solution I can use multiple application with Startup Projects, which use the same modules. For example: My Main Application Startup Project and Test Application Project.

    Thanks.

  • re: XAPs Minifier. Avoiding Problem With Multiple Startup Objects

    Thanks, Denis!
    I will think out what I can do

Comments have been closed on this topic.