Trouble converting a console application to a WPF application in VS 2010

10,390

Solution 1

The easiest method would be to create a new WPF application and move the code. The best method would be to push the logic into a business library so you can easily use a console application and a wpf application as the presentation technology for the business logic, because WPF is a user interface technology, not an application type.

From a geek standpoint, if you want to solve the hard way to learn, I would create a separate WPF applicationn and examine the proj file. Most likely there is some little bit in there that makes things work as WPF, as well as some missing references.

Solution 2

I did convert my console application to WPF application by below steps
All you need to do is change project type guid in project file. 1. Add reference to PresentationCore assembly
2. Add reference to PresentationFramework assembly
3. Add reference to WindowsBase assembly
4. Go to Project properties then on Application tab change output type to Windows Application.
5. Create new project of WPF application and copy App.xaml and MainWindow or Anyother xaml files to your console application.
And last thing to get Resource dictionary when you right click on project and select add Open your project file e.g. WpfApp.csproj on notepad And add

**<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>**

in first PropertyGroup element after FileAlignment , it should look like

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>9.0.30729</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{58688A7B-82F4-4229-949A-C4249DAB43FC}</ProjectGuid>
<OutputType>WinExe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>ConsoleApplication1</RootNamespace>
<AssemblyName>ConsoleApplication1</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<StartupObject>
</StartupObject>

Save it. if your project is open in visual sutiod it will ask to reload project, click yes. Thats it, it is now working as WPF Application and you will find Add all menus which are available on WPF application.

Solution 3

I started to edit the answer from RAJ (as the steps listed there are incomplete), but my changes were a little too drastic, so I decided to post my answer separately.

You can convert a console application to a WPF application with these steps:

  1. Add reference to the following assemblies:
    • PresentationCore
    • PresentationFramework
    • WindowsBase
    • System.Xaml
  2. Open Project properties. On Application tab, change output type to Windows Application.
  3. Create new WPF application (e.g. name it "tempWPF") and copy App.xaml and MainWindow.xaml (along with the backing .xaml.cs files for both) to your console application.
    • Replace all occurences of "tempWPF" with the name of your console project.
  4. In the Properties window for App.xaml, set Build Action to ApplicationDefinition (as described here).
  5. Remove/Delete Program.cs file from the project. (After relocating any custom logic from that file as necessary, of course).
  6. Change the project type guid. To do this, edit your project file (e.g. WpfApp.csproj) either in Notepad++ or directly in Visual Studio, adding the following line somewhere in first PropertyGroup element:

    <ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
    
  7. Reload your project in Visual Studio.

Solution 4

Like everyone else said, the practical way of making a WPF is to just start a new WPF application project and adapt your console code. However, Gregory A Beamer is correct at pointing out that you may want the geek solution.

Maybe this would be the place to look for the right option:

In the menu, hit Tools >> Customize. In the "Customize" dialog, hit the "Commands" tab, and then click on the "Editor Context Menus" radio button. You can now edit all the context menus in VS. I assume these context menus get automatically customized for each application type. The context menu used to add a resource dictionary to the project should be somewhere in here. Though changing them manually may be a pain in the behind, the geek point is that they're still there.

Share:
10,390
bshacklett
Author by

bshacklett

Updated on June 14, 2022

Comments

  • bshacklett
    bshacklett almost 2 years

    I created a console application that I later decided would function better as a WPF application. I changed the output type to Windows Application and added what I believe are the necessary references. Unfortunately, right-clicking on the project does not allow me to add a Resource Dictionary or many other WPF types. What have I missed?

  • Christian Droulers
    Christian Droulers about 10 years
    I also had to add "new app().Run(new MainWindow())" in my Program.cs to get it to start. [STAThread] was required as well.
  • Naresh
    Naresh over 9 years
    Without ProjectTypeGUID attribute, I was manually able to add files and work. Once I added Project GUID the VS solution explorer context menu and the project template started showing the Window and other wpf types