Saturday, May 2, 2009

Where did my WPF Namespace go?

At work we are using a WPF gui but calling it from some old code. To get this to work we had to work around a few things with the application and the main window showing up. But this post is not about that. The new project that I am working on now is moving that same WPF application back to be the main app that gets run. But I found a small namespace issue when getting this to run.

Since the App.xaml was not being run, the old code actually called the App class directly and then created and showed the main window. During this time there was a new policy in place where we changed the namespaces to include the company name first. So MyProduct changed to MyCompany.MyProduct everywhere.

During my converting this WPF to be stand-alone again, I changed the project type back to WinExe and put a breakpoint in the constructor of the App class. The breakpoint never got hit and it went directly to the Window1 constructor. I had some important resource calls in that constructor that had to be called before things in the rest of the code were called. Why was it not hitting the breakpoint??

So in the Window1 class I got a copy of the current application and tried to call something on it to allow me to setup the resources that I needed. But when I did this:

MyCompany.MyProduct.App app = (MyCompany.MyProduct.App)Application.Current ;

I got an error that said that it could not convert MyProduct.App to MyCompany.MyProduct.App. This was strange. It took a while to figure out that the App.xaml file had a different namespace than the App.xaml.cs file. The xaml file never got the MyCompany name added to the namespace. This worked because of a could things. We were calling the App class directly and not letting the project file select it for us. The second thing is that if you look at the new App class in a new WPF project you will see that the constructor is empty. This means that there is nothing really connecting the xaml created partial class and the xaml.cs part of it. For any window class the constructor has a call to InitializeComponent. If the namespace gets out of sync the xaml.cs for the window will give an error on compiling. After fixing the namespace in the App.xaml file, all was good. The program ran and then I had a lot of other work to do. But that is a story for another time.

No comments: