No creation of a WPF window in a DLL project?

WpfDllWindow

Wpf Problem Overview


Is there a reason why Visual Studio won't let me create a WPF window in a DLL project?

I "solved" it by creating a window in an Application Project and copying it to my DLL project. I also found that I could just create a UserControl and change the base class to "Window".

But if I had to do it this way, it's maybe because I shouldn't do it...

Wpf Solutions


Solution 1 - Wpf

Make sure the project type is WPF User Control Library when you create your project.

If it isn't then no sweat, just edit the csproj file and make sure the <ProjectTypeGuids> element under Project/PropertyGroup contain the following GUIDs

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

Also, make sure you reference PresentationFramework and System.Xaml in your project, or you will not get any WPF in your code.

Solution 2 - Wpf

You can try adding new WPF User Control Item and change that to Window.

Add New Item->WPF->User Control

In XAML:

Change <UserControl> tag as <Window>

In CS:

Change base class from System.Windows.Controls.UserControl to System.Windows.Window.

Solution 3 - Wpf

I do it this way:

  1. create "WPF Application"
  2. remove App.xaml
  3. change Project properties -> Application Output type: to Class Library (originally there is Windows Application)

Otherwise you will get errors:

"Library project file cannot specify ApplicationDefinition element"

"The project file contains a property value that is not valid"

Solution 4 - Wpf

What do you mean that Visual Studio won't let you create a WPF window in a DLL project? Do you mean that if you right click the project, there is no option to add a Window there?

If that is the case, I think that means that you created a project type that isn't a WPF project type. I encountered something similar a while back when I wanted to upgrade a WinForms project to use WPF instead - see this question for more information.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAntoine JeanrichardView Question on Stackoverflow
Solution 1 - WpfFlorian DoyonView Answer on Stackoverflow
Solution 2 - WpfelectronView Answer on Stackoverflow
Solution 3 - WpfViliamView Answer on Stackoverflow
Solution 4 - WpfAndyView Answer on Stackoverflow