I’m currently working on some cool demos for PDC09. Unfortunately, I can’t talk about them yet, but I can tell you some interesting things that I have found along the way. Since I do a lot of different development on a lot of different projects, most of my projects are built inside a VM. This makes it really easy to keep the projects separate and keeps the host machine clean. However, Hyper-V seems to kill the performance of graphics intensive tasks. So, Blend work is really slow. And besides, I’m among other things running some things that work with Azure, which is kind of slow. So I have opted for a solution where I share the Silverlight project between my VM and my host, making it possible for me to do the graphically intensive things on the host, but keep the source and source control in the VM.
So, what has this got to do with naming issues. Well, while waiting for the VM to be built (I don’t have to do that myself…:) ), I started development on my host machine instead of just waiting. However, the solution I started working on had the wrong namespace. So I had to rename the namespace. I did it by simply doing a find and replace, which seemed to work very well. There were a few things that I needed to handle manually, but generally it worked nicely. The project compiled and I opened up my browser to view it…and pow! I get this error message saying:
Line: 56
Error: Unhandled Error in Silverlight Application
Code: 2103
Category: InitializeError
Message: Invalid or malformed application: Check manifest
I really didn’t have time with this, but I had to rename the namespace. Hmm… Google to the rescue… Ahh…It is the message you get when assemblies are missing in the Xap. So I opened up the Xap and check the manifest and included assemblies. But everything was fine…
However, the reason is in the manifest. But it isn’t missing assemblies. The manifest contains a reference to the entry point for the application. When changing namespace this isn’t updated. Even if you update the project’s default namespace. In VS, there is another setting that needs updating. Under the Silverlight tab in the project properties, there is a setting called “Startup object”. It is a combo box that makes it possible to select the entry point. This looks fine even if you rename the namespace. However, if you you open the combo box, you will see that the name of the App class actually changes. After that, you can just recompile and the manifest will have the updated reference…
The next naming problem has to do with templated controls. When working with Silverlight applications in VS 2008, there is a new item type template called “Silverlight Templated Control”. It is a great template that will create a class that inherits from Control and that sets the DefaultStyleKey in the constructor. It also creates a Generic.xaml file in a folder named Themes. This is great, that means that I don’t have to manually create a Generic.xaml file with the ResourceDictionary used for the default templates.
So, I created the control functionality and the template and started the application…and once again pow!
AG_E_UNKNOWN_ERROR [Line: 8 Position: 30]
That is such a helpful error message! So, what is the problem… Well, I created my control in a subfolder called Controls. That means that the generated control class gets placed in a separate namespace that ends with Controls. The Generic.xaml file on the other hand is auto generated by some brilliant function that doesn’t care about this. Instead it adds a ResourceDictionary that looks like this
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:SilverlightProject">
<Style TargetType="local:MyControl">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:MyControl">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
If you look carefully, the xml namespace “local” that is used in the Style is set to “clr-namespace:SilverlightRenaming”, which doesn’t really correspond to the class that looks like this
namespace SilverlightRenaming.Controls
{
public class MyControl : Control
{
public MyControl()
{
this.DefaultStyleKey = typeof(MyControl);
}
}
}
So the useless error message AG_E_UNKNOWN_ERROR is actually pretty easy to solve as soon as you find it…like all errors… So after a quick change of the xml namespace, the application is up and running.