Thursday, February 18, 2010

WPF Errors - InitializeComponent doesn't exist, etc

I've been learning how to write WPF applications, and I was going through activities in a book.  One of the activities is to create an ImageViewer.

I modified the Window1.xaml file and changed the first line to so that the class is called "ImageViewer.Window1".  I then called a FolderoOpenMenuItem_Click event from the xaml file, and defined it in the Window1.xaml.cs file.  I also had an InitializeComponent in the constructor.  When I compiled the application I got the following errors:

- The name 'InitializeComponent' does not exist in the current context
- 'Windows1.Window1' does not contain a definition for 'FolderOpenMenuItem_Click' and no extension method for 'FolderOpenMenuItem_Click' accepting a first argument of type 'Window1.Window1' could be found (are you missing a using directive or an assembly reference?)

This didn't make sense to me because I had defined the function, so why was it saying that nothing existed.  Also, I didn't know where the InitializeComponent function was defined.

After looking on the web, I found the following link -

Basically the issue is that in the obj\Debug folder is a file Window1.g.cs and here is where InitializeComponent is defined.  The namespace I had was wrong, and once I changed the namespace to ImageViewer everything was able to build and work.

However, I was still concerned that if I changed the build configuration to release there would be problems.  But when I did so there were no problems.  But I still didn't like this solution, so what I did was right-click on the project and changed the Assembly Name and Default Namespace to ImageViewer and did a global replace for the solution on anything that said WPFapplication1 to ImageViewer.  I was able to build it and everything seemds to now work.

No comments: