How to: Change your namespace in a Silverlight application without breaking everything

Posted Friday, July 23, 2010 3:16 PM by CoreyRoth

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.

SilverlightProjectProperties1

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.

SilverlightStatusAppXaml1

I have highlighted the section, I need to change, x:Class.  I’ll change it to DotNetMafia.Silverlight.Test like we see below.

SilverlightStatusAppXaml2

Now, we just need to change the namespace in the code behind file App.xaml.cs.

SilverlightStatusAppXamlCs1

Now, we have to do the same thing to MainPage.xaml.  Change the namespace here on x:Class as well.

SilverlightStatusMainPageXaml1

Then, you will change the namespace of the code behind file MainPage.xaml.cs just liked we did before.

SilverlightStatusMainPageXamlCs1

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.

SilverlightStartupObjectErrorToolbar

Clicking on the error, you can see the following details.

SilverlightStartupObjectError

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
Code: 2103   
Category: InitializeError      
Message: Invalid or malformed application: Check manifest    

Line: 54
Char: 13
Code: 0
URI:
file:///C:/Code/SilverlightApplication1/Bin/Debug/SilverlightApplication1TestPage.html

Remember, I said we had to change an additional setting in the project properties?

SilverlightProjectProperties2

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.

SilverlightProjectProperties3

At this point your application should compile and run.  Now I can view my beautiful Hello World Silverlight application.

 SilverlightNamespaceChangeWorking

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.

Comments

# Twitter Trackbacks for SharePoint Buzz - How to: Change your namespace in a Silverlight application without breaking everything [dotnetmafia.com] on Topsy.com

Pingback from  Twitter Trackbacks for                 SharePoint Buzz - How to: Change your namespace in a Silverlight application without breaking everything         [dotnetmafia.com]        on Topsy.com

# re: How to: Change your namespace in a Silverlight application without breaking everything

Monday, July 26, 2010 8:39 AM by michaud

Why not give it the correct name in the first place????

# re: How to: Change your namespace in a Silverlight application without breaking everything

Monday, July 26, 2010 8:49 AM by CoreyRoth

@michaud Well of course you can.  However, I've been in scenarios where people want the project folder names a certain way (i.e.: they set up a tree instead of naming it after the namespace).  And then again sometimes people just change their minds. :-)

# re: How to: Change your namespace in a Silverlight application without breaking everything

Monday, April 25, 2011 5:00 AM by dali

thanks

# re: How to: Change your namespace in a Silverlight application without breaking everything

Monday, April 25, 2011 9:23 AM by Nacho

Thanks man! This post was very Useful!

# re: How to: Change your namespace in a Silverlight application without breaking everything

Monday, October 14, 2013 7:29 AM by Daniel

Perfect, this was excatly what I was looking for!

Leave a Comment

(required)
(required)
(optional)
(required)