Thursday, September 23, 2010

Gotcha! Blend 4 & namespaces

When we first upgraded from Silverlight 3 to Silverlight 4 (and thus Blend 3 to Blend 4) we started noticing some issues in Blend we'd never seen before. Any template we created that contained a ValueConverter reference would not render and would show "invalid xaml". Additionally, none of the templates that contained one of our self-contained xaml "image" canvases we used for icons would render either.

After a lot of pain and anguish I figured out the issue.

Basically, in Blend 4, namespaces are resolved purely based on folder structure, NOT by what namespace they are actually in. Blend 3 resolved based on the actual namespace.

When we first started working on this project we didn't have the same level of organization that we eventually came up with. We made a Common folder for some class files (including the ValueConverters) and an Images folder for our xaml image canvases and then moved the files into these folders from the root where they had been originally created. We did not change the namespace when we did this.

Thus, in Blend 3, which resolved namespaces based on, well, reality -- there was no problem locating the files. In Blend 4, which relies on the folder structure -- the files could not be found because it was insisting that if the file was in the Root/Common folder it MUST be in the Root.Common namespace. When I changed the namespace of the file to match the folder structure and changed the references in Blend to point to the new namespace, the files were resolved and the Blend errors went away.

People creating new projects in a pre-determined folder structure won't have this issue because when you create the file in the Root.Common folder it automatically puts it in the Root.Common namespace -- but this is a big gotcha for people reorganizing existing projects. And, since the code actually works fine when you compile and run it -- it's not immediately apparent until you try to use Blend that you've created a problem just by moving the file to a subfolder.

While in general I favor clear organization where the folder structure and namespaces match, I do not like this change. I wasted a LOT of time figuring it out. Hopefully I can save someone else the time.

0 comments: