Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't Visual Studio want me to add a new window to my WPF project?

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?

like image 999
Benjol Avatar asked Sep 27 '09 20:09

Benjol


People also ask

How do I add apps to XAML to WPF project?

Solution 1Go to add new items -> online templates and then choose 'Silverlight Application Class'. This will add a new app. xaml for you.

How do I display a WPF window?

When a Window is created at run-time using the Window object, it is not visible by default. To make it visible, we can use Show or ShowDialog method. Show method of Window class is responsible for displaying a window.


2 Answers

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> 
like image 145
Warty Avatar answered Oct 17 '22 15:10

Warty


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.

like image 37
Drew Noakes Avatar answered Oct 17 '22 13:10

Drew Noakes