40

Is it possible to build a Class Library dll which also includes WPF forms? When I try to build one I get following errors:

Error 1 Library project file cannot specify ApplicationDefinition element.
Error 2 The project file contains a property value that is not valid.

If I set my project as Windows Application it compiles & runs tho. Is there a way to get arround this? I'm using VS2010 & C# .NET 4.0 if that might be of any interest to you guys.

Thanks

Marcus Mangelsdorf
  • 2,852
  • 1
  • 30
  • 40
Jens
  • 3,249
  • 2
  • 25
  • 42

6 Answers6

45

Choose WPF Custom Control Library or WPF User Control Library when creating/adding new project to your solution.

decyclone
  • 30,394
  • 6
  • 63
  • 80
  • Actually it's more of a mixed library. It contains regular classes and functionalities, together with some forms. Will that work? – Jens Dec 14 '10 at 08:10
  • 9
    Is there any way of doing this AFTER I already created it as a normal Class Library? I'm looking at it's properties but neither WPF Custom Control Library or WPF User Control Library can be picked from there.. – Jens Dec 14 '10 at 08:16
  • 10
    Yes, to get rid of this error, select App.xaml in your solution explorer and delete it. – decyclone Dec 14 '10 at 08:20
  • 5
    I already have a solution with several projects. The project to which I've added the WPF Form doesn't haven an App.xaml file.. – Jens Dec 14 '10 at 08:26
  • 29
    There must be a file in your project whose `Build Action` is set to `ApplicationDefinition`. You have to remove that file or change its `Build Action`. – decyclone Dec 14 '10 at 08:27
  • 1
    @deycyclone, by the upvotes, that `Build Action` must work for some people. But not for me -- after all it contradicts error #1 in the OP. Mikl X's advice worked fine for me. – Adrian Ratnapala Jan 17 '14 at 14:40
30

Change the build action of your UserControl from ApplicationDefinition to Page.

Mikl X
  • 1,199
  • 11
  • 17
11

Remove the App.xaml and App.xaml.cs in your WPF Project. Then you will be able to run the project with an output type of Class Library.

Bobby
  • 467
  • 4
  • 13
user2493490
  • 109
  • 1
  • 2
  • Ran into this after creating a Wix Custom BA, then changing the WPF project from Application to Class Library. Deleted the App.xaml.* files, then it compiled immediately! – Lynn Crumbling Jan 07 '15 at 22:34
8

This is also occur when we copy paste an Image to ClassLibrary Project.

Change the Build Action Property of that image to resources

aditya potdar
  • 357
  • 3
  • 8
2

problems after copying XAML controls from WPF Application to class library

The first answer work for me. I changed App.xaml from ApplicationDefinition to Page and worked

Community
  • 1
  • 1
1

Extending @2493490 answer.

Right click on the app.xaml file and exclude it from the project. Change the project output type to in project properties to Class Library. Click on the run icon, the solution will successfully build but will not run/execute any code. A error message will be displayed explaining as such.

IbrarMumtaz
  • 4,235
  • 7
  • 44
  • 63