在VS 2010中,将控制台应用程序转换为WPF应用程序的问题。

时间:2022-03-08 14:28:07

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?

我创建了一个控制台应用程序,后来我决定将其作为WPF应用程序运行得更好。我将输出类型更改为Windows应用程序,并添加了我认为必要的引用。不幸的是,右键单击项目不允许添加资源字典或许多其他WPF类型。我错过了什么?

4 个解决方案

#1


6  

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.

最简单的方法是创建一个新的WPF应用程序并移动代码。最好的方法是将逻辑推入业务库,以便您可以轻松地使用控制台应用程序和wpf应用程序作为业务逻辑的表示技术,因为wpf是一种用户界面技术,而不是应用程序类型。

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.

从极客的角度来看,如果您想解决学习的难题,我将创建一个单独的WPF应用程序并检查proj文件。最有可能的是,其中有一些使事情以WPF的形式运行,以及一些丢失的引用。

#2


10  

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

我确实通过以下步骤将控制台应用程序转换为WPF应用程序,您只需在项目文件中更改项目类型guid。1。添加对PresentationCore程序集2的引用。添加对PresentationFramework组装3的引用。添加对WindowsBase程序集的引用4。转到“项目属性”,然后在“应用程序”选项卡上将输出类型更改为“Windows应用程序”。创建WPF应用程序的新项目,并将App.xaml和MainWindow或其他xaml文件复制到控制台应用程序。最后要获取资源字典当你右键单击项目并选择add打开你的项目文件,例如WpfApp。在记事本上加上csproj

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

in first PropertyGroup element after FileAlignment , it should look like

在filealign之后的first PropertyGroup元素中,应该是这样的

<?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.

保存它。如果您的项目是在visual sutiod中打开的,它将要求重新加载项目,单击yes。就是这样,它现在作为WPF应用程序工作,您将发现添加所有在WPF应用程序上可用的菜单。

#3


4  

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.

我开始编辑来自RAJ的答案(因为上面列出的步骤不完整),但是我的修改有点太激烈了,所以我决定单独发布我的答案。

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

您可以通过以下步骤将控制台应用程序转换为WPF应用程序:

  1. Add reference to the following assemblies:
    • PresentationCore
    • PresentationCore
    • PresentationFramework
    • PresentationFramework
    • WindowsBase
    • WindowsBase
    • System.Xaml
    • System.Xaml
  2. 添加对以下程序集的引用:PresentationCore PresentationFramework WindowsBase System.Xaml。
  3. Open Project properties. On Application tab, change output type to Windows Application.
  4. 打开项目属性。在Application选项卡上,将输出类型更改为Windows应用程序。
  5. 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.
    • 用控制台项目的名称替换所有出现的“tempWPF”。
  6. 创建新的WPF应用程序(例如,命名为“tempWPF”),并复制App.xaml和MainWindow。xaml(与后面的.xaml一起)。两者的cs文件)到您的控制台应用程序。用控制台项目的名称替换所有出现的“tempWPF”。
  7. In the Properties window for App.xaml, set Build Action to ApplicationDefinition (as described here).
  8. 在App.xaml的Properties窗口中,将构建动作设置为ApplicationDefinition(如本文所述)。
  9. Remove/Delete Program.cs file from the project. (After relocating any custom logic from that file as necessary, of course).
  10. 删除/删除程序。cs文件从项目。(当然,在需要从该文件中重新定位任何自定义逻辑之后)。
  11. 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:

    更改项目类型guid。为此,可以在Notepad++或直接在Visual Studio中编辑项目文件(例如,WpfApp.csproj),在first PropertyGroup元素的某个地方添加以下代码:

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

    在Visual Studio中重新加载项目。

#4


1  

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.

就像每个人说的,创建WPF的实际方法是启动一个新的WPF应用程序项目并修改您的控制台代码。不过,Beamer Gregory指出,你可能需要一个极客解决方案。

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.

在菜单中,点击工具>>定制。在“自定义”对话框中,单击“命令”选项卡,然后单击“编辑器上下文菜单”单选按钮。您现在可以编辑vs中所有的上下文菜单,我假设这些上下文菜单会自动为每个应用程序类型定制。用于向项目添加资源字典的上下文菜单应该在这里的某个地方。尽管手动更改它们可能是一件痛苦的事,但极客的观点是它们仍然存在。

#1


6  

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.

最简单的方法是创建一个新的WPF应用程序并移动代码。最好的方法是将逻辑推入业务库,以便您可以轻松地使用控制台应用程序和wpf应用程序作为业务逻辑的表示技术,因为wpf是一种用户界面技术,而不是应用程序类型。

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.

从极客的角度来看,如果您想解决学习的难题,我将创建一个单独的WPF应用程序并检查proj文件。最有可能的是,其中有一些使事情以WPF的形式运行,以及一些丢失的引用。

#2


10  

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

我确实通过以下步骤将控制台应用程序转换为WPF应用程序,您只需在项目文件中更改项目类型guid。1。添加对PresentationCore程序集2的引用。添加对PresentationFramework组装3的引用。添加对WindowsBase程序集的引用4。转到“项目属性”,然后在“应用程序”选项卡上将输出类型更改为“Windows应用程序”。创建WPF应用程序的新项目,并将App.xaml和MainWindow或其他xaml文件复制到控制台应用程序。最后要获取资源字典当你右键单击项目并选择add打开你的项目文件,例如WpfApp。在记事本上加上csproj

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

in first PropertyGroup element after FileAlignment , it should look like

在filealign之后的first PropertyGroup元素中,应该是这样的

<?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.

保存它。如果您的项目是在visual sutiod中打开的,它将要求重新加载项目,单击yes。就是这样,它现在作为WPF应用程序工作,您将发现添加所有在WPF应用程序上可用的菜单。

#3


4  

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.

我开始编辑来自RAJ的答案(因为上面列出的步骤不完整),但是我的修改有点太激烈了,所以我决定单独发布我的答案。

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

您可以通过以下步骤将控制台应用程序转换为WPF应用程序:

  1. Add reference to the following assemblies:
    • PresentationCore
    • PresentationCore
    • PresentationFramework
    • PresentationFramework
    • WindowsBase
    • WindowsBase
    • System.Xaml
    • System.Xaml
  2. 添加对以下程序集的引用:PresentationCore PresentationFramework WindowsBase System.Xaml。
  3. Open Project properties. On Application tab, change output type to Windows Application.
  4. 打开项目属性。在Application选项卡上,将输出类型更改为Windows应用程序。
  5. 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.
    • 用控制台项目的名称替换所有出现的“tempWPF”。
  6. 创建新的WPF应用程序(例如,命名为“tempWPF”),并复制App.xaml和MainWindow。xaml(与后面的.xaml一起)。两者的cs文件)到您的控制台应用程序。用控制台项目的名称替换所有出现的“tempWPF”。
  7. In the Properties window for App.xaml, set Build Action to ApplicationDefinition (as described here).
  8. 在App.xaml的Properties窗口中,将构建动作设置为ApplicationDefinition(如本文所述)。
  9. Remove/Delete Program.cs file from the project. (After relocating any custom logic from that file as necessary, of course).
  10. 删除/删除程序。cs文件从项目。(当然,在需要从该文件中重新定位任何自定义逻辑之后)。
  11. 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:

    更改项目类型guid。为此,可以在Notepad++或直接在Visual Studio中编辑项目文件(例如,WpfApp.csproj),在first PropertyGroup元素的某个地方添加以下代码:

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

    在Visual Studio中重新加载项目。

#4


1  

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.

就像每个人说的,创建WPF的实际方法是启动一个新的WPF应用程序项目并修改您的控制台代码。不过,Beamer Gregory指出,你可能需要一个极客解决方案。

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.

在菜单中,点击工具>>定制。在“自定义”对话框中,单击“命令”选项卡,然后单击“编辑器上下文菜单”单选按钮。您现在可以编辑vs中所有的上下文菜单,我假设这些上下文菜单会自动为每个应用程序类型定制。用于向项目添加资源字典的上下文菜单应该在这里的某个地方。尽管手动更改它们可能是一件痛苦的事,但极客的观点是它们仍然存在。