Lately I've been upgrading my Winforms CAB applications to use the Windows Presenation Framework version of the Composite Application Block. This post will explain how you can do an "in place" conversion of your Winform project to WPF.
The normal way to convert a project to WPF would be backup your Winform project, create a new WPF project, recreate all your screens as WPF Windows (or UserControls) and copy and paste the non-visual code from the backup into the new WPF project. That would be the recommended way for most projects, especially if you have many forms or user controls to upgrade. However, I have several projects that have but one view and all that copying and renaming and adding projects seemed like too much rigamarole. Besides, I liked the names of my projects and didn't want to create new projects with new namespaces and get all the naming right. Plus, I would have to add references, etc. So I decided to perform a manual conversion of some projects to use WPF. Here is how I did it.
First, I will assume that you have your WPF version of your Shell project already created (use the BankTeller WPF example as a guide). This post will only cover the conversion of a module project because its simpler and it shows the general philosophy and gotchas of the conversion.
First, start by ripping out the references to Winforms. Remove the references to System.Windows.Forms and System.Drawing. Add references to WindowsBase, PresentationCore and PresentationFramework. Rename your winforms views to get them out of the way. Create new WPF UserContols to replace the old Winforms views. Recreate your new UI using WPF controls. This could take some effort but if your views are simple and slim then most of your code will be in the Presenter/Controller and it will be a reasonably easy task to recreate the new UI in WPF. Remove the Winform view from the project when you no longer need it.
You can now attempt to build. You should get a cryptic compilation error message that says "The name 'InitializeComponent' does not exist in the current context". Don't worry, you didn't do anything wrong. There is one additional step to complete. In order for msbuild to compile your .NET 3.0 project, it needs to know about the WinFX build configuration. This step, which you will do manually, would normally be done automatically when you create a new WPF project instead of converting in-place like I am showing. With one of the files from your project open for editing, right click the name of the file in the editing tab and select "Open Containing Folder". This will open Windows Explorer in your project folder. Open your .proj file with your favorite editor such as Notepad. Down near the bottom of the file, you will see a line that says:
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
Add the following line right below it:
<Import Project="$(MSBuildBinPath)\Microsoft.WinFX.targets" />
Save the change and reload the project. Build succeeded!
Image may be NSFW.Clik here to view.
