WPF : Renaming the Main Form

Finally, after reading books about WPF I started to actually work with it. As usual, I start with complaints 🙂 It turns out that renaming a WPF form, especially main is a) inevitable and b) tedious.

By default, when you create an WPF application, the wizard creates a main window class with an undescriptive name of Window1. This is a problem in itself, since your never really want to have a class named Window1. Back in the days of MFC the wizards used to ask what class name you want, but this craft has been lost in the 21st century.

So, any sane person would want to rename Window1 to something like MainWindow. In Windows Forms you just had to rename the form in Solution Explorer and it did the trick. In WPF you need to edit quite a few files:

* Rename the class using the refactoring menu (or Shift+Alt+F10 shortcut)
* Fix the class summary that says “Interaction logic for Window1.xaml”
* Fix class name in MainWindow.xaml file
* Fix the start URI in App.xaml file

This is the price you pay for programming in XML. Automatic refactoring tools got pretty good in refactoring the code, but they can’t possibly know if you have your class names stashed in some XML files. References from XML to code can easily get out of sync, and you are on your own in checking that. If you are lucky, it will blow up during debugging. If you are not lucky, it will blow up on customer’s desktop.

Someone must say that this is no big deal – why rename at all? The program works just fine with Window1. The answer is, the only safe way to evolve code is gradual refactoring, and renaming something is one of the basic refactoring steps. If renaming is hard, it hinders everything else.

Leave a Reply

Your email address will not be published. Required fields are marked *