问题
Maybe a stupid question, but when I add item to a WPF project, Visual Studio only offers me UserControl, and not Window. Is this trying to encourage me to do MVVM, is my setup broken, or is there some other reason I haven't thought of?
回答1:
Your project is probably configured as a WinForms project, or possibly as a class library. If it's created as either of these, you are only able to add a WPF UserControl, unfortunately.
Of course, there's no technical reason for this limitation, so you can copy/paste one from another project or recreate/change your project to be a WPF project.
I'm not sure what you change exactly to make it a WPF project in VS's eyes. You might try creating a new project and diffing it to your current project. You will most likely have to do some text editing on your .csproj
file.
回答2:
Add this to your first <PropertyGroup>
element in your csproj:
<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
Here's an example:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{24793F93-0FD8-4EC9-B1D2-028DB489B10D}</ProjectGuid>
<OutputType>WinExe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>nest_spawner</RootNamespace>
<AssemblyName>nest-spawner</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
Becomes:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{24793F93-0FD8-4EC9-B1D2-028DB489B10D}</ProjectGuid>
<OutputType>WinExe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>nest_spawner</RootNamespace>
<AssemblyName>nest-spawner</AssemblyName>
<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
回答3:
Try running devenv /installvstemplates
to reset your installed templates.
I've used this before when the WPF User Control mysteriously disappeared from my add new list.
In my WPF project, when I click Add I get the following options (which includes Window).
So I'm afraid it's your setup.
回答4:
don't use Visual studio 2015 Microsoft Blend... to open your project.. open your project in Visual Studio - Microsoft Visual Studio ,,, see in title bar
来源:https://stackoverflow.com/questions/1484403/why-doesnt-visual-studio-want-me-to-add-a-new-window-to-my-wpf-project