wpf 在添加新项目窗口中找不到 ResourceDictionary

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12558076/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 05:30:36  来源:igfitidea点击:

Can't find ResourceDictionary in Add New Item Window

wpfwpf-controlsresourcedictionary

提问by Cuero

When I create a new WPF project I can find ResourceDictionary in Add New Item Window. But I've another Project I can't find that and I don't know why. Only UserControl but no ResourceDictionary

当我创建一个新的 WPF 项目时,我可以在添加新项目窗口中找到 ResourceDictionary。但我有另一个项目我找不到,我不知道为什么。 Only UserControl but no ResourceDictionary

UPDATE: The project was for .net 3.5 originally, but now it also has a version for .net 4.0. It means there're two .sln files (one for 3.5 and the other for 4.0) both for the same project.

更新: 该项目最初是针对 .net 3.5 的,但现在它也有针对 .net 4.0 的版本。这意味着同一个项目有两个 .sln 文件(一个用于 3.5,另一个用于 4.0)。

采纳答案by Trevor Elliott

First of all, I hope you realize this shouldn't stop you since you can easily add any file you want to a project, either from your file system or by copying it from another project. The Add New Item window is just for convenience.

首先,我希望您意识到这不会阻止您,因为您可以轻松地将任何您想要的文件添加到项目中,无论是从您的文件系统还是通过从另一个项目复制它。添加新项目窗口只是为了方便。

Secondly, when you added the new project to your solution, which project template did you choose? The project template determines the initial set of referenced assemblies that project has. A WPF project makes references to the WPF libraries (WindowsBase, PresentationCore, etc.).

其次,当您将新项目添加到您的解决方案时,您选择了哪个项目模板?项目模板确定项目具有的初始引用程序集集。WPF 项目引用 WPF 库(WindowsBase、PresentationCore 等)。

Visual Studio uses your referenced assemblies to generate the possible items you see in the Add New Items dialog.

Visual Studio 使用您引用的程序集生成您在“添加新项目”对话框中看到的可能项目。

So I'm assuming you added some other type of project, such as a basic Class Library. You could manually add the references to the WPF assemblies using the Add Reference dialog. Or you could re-create the project as a WPF Custom Control Library.

所以我假设您添加了一些其他类型的项目,例如基本类库。您可以使用“添加引用”对话框手动添加对 WPF 程序集的引用。或者,您可以将项目重新创建为 WPF 自定义控件库。

回答by MatrixManAtYrService

Add the following line to Project.csproj

将以下行添加到 Project.csproj

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

It should be a child of the <PropertyGroup>tag, like so:

它应该是<PropertyGroup>标签的子项,如下所示:

    <Project>
      <PropertyGroup>
        ....
        <ProjectTypeGuids>{guids};{go};{here}</ProjectTypeGuids>
        ...
      </PropertyGroup>
    ...
    </Project>
    <Project>
      <PropertyGroup>
        ....
        <ProjectTypeGuids>{guids};{go};{here}</ProjectTypeGuids>
        ...
      </PropertyGroup>
    ...
    </Project>

This postdoes a good job of explaining why this works.

这篇文章很好地解释了为什么会这样。