The first thing I do when creating a new project in Visual Studio (regardless of type) is change the project name, assembly name, and default namespace. I like for the names of all these to be consistent. However, I have noticed in Silverlight, you must make changes in five different places for everything to work alright otherwise you will get errors. I’m no Silverlight expert, but I thought this post would be useful for people like me who only dabble in it from time to time.
When you create your new Silverlight project, right click on the project name and bring up its properties. Go ahead and change the default namespace and assembly name just like you would in any other project.
In my case, I change from a namespace of SilverlightApplication1 to DotNetMafia.Silverlight.Test. Here you can see the new assembly name and default namespace. There are two other options to set here, but we will have to come back to it.
At this point, we want to correct the namespace in the existing classes. Let’s start with App.xaml.
I have highlighted the section, I need to change, x:Class. I’ll change it to DotNetMafia.Silverlight.Test like we see below.
Now, we just need to change the namespace in the code behind file App.xaml.cs.
Now, we have to do the same thing to MainPage.xaml. Change the namespace here on x:Class as well.
Then, you will change the namespace of the code behind file MainPage.xaml.cs just liked we did before.
At this point, your code will compile. However, it will not run. If you try to debug it you will likely get a blank page and if you are using Internet Explorer, you will probably see a script error in the toolbar.
Clicking on the error, you can see the following details.
I have to say they have really improved the way you view script errors. Here is the text of the error.
Message: Unhandled Error in Silverlight Application
Message: Invalid or malformed application: Check manifest
Remember, I said we had to change an additional setting in the project properties?
This is the problem here. We change the startup objects namespace but never update the project properties to match. You should be able to pick the new namespace of your startup object from the list. This is also a good time to change the name of your .XAP file if you are so inclined. Here is what my project properties looks like when I am done.
At this point your application should compile and run. Now I can view my beautiful Hello World Silverlight application.
Great app huh? Any how, I hope this helps should you encounter the error above or run into issues changing your namespace. It’s pretty simple to do, but you’ll definitely get errors if you don’t get all of your changes made.