Problems after copying XAML controls from WPF Application to a class library

In the process of refactoring some code, I encountered several build errors such as these:

Library project file cannot specify ApplicationDefinition element.

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

How can these errors be resolved?


Solution 1:

When you copy and paste files either within a project or to another project, Visual Studio has a nasty habit of not keeping the same BuildAction property. It often changes the build action to a seemingly random value, e.g. ApplicationDefinition, which causes that build error.

Check (in the Visual Studio properties window with the file selected in the Solution Explorer) that each of your .xaml files have a BuildAction property of Page and your code files have a BuildAction property of Compile.

Solution 2:

You may also see this even though your build actions are set correctly. For example, if you start your project up as an Application, and then later switch it to be a Class Library. In which case you must also remember to delete App.xaml (and the corresponding App.cs).

Solution 3:

This is also when we copy paste an Image/XAML File to ClassLibrary Project.

Change the Build Action Property of that image to resources

Solution 4:

The build action on the Xaml control must be changed from ApplicationDefinition to Page (this property can be accessed by right clicking on the control in the Solution Explorer treeview and selecting properties).

Solution 5:

2 years later and I came searching with the same question, but was only able to use the above comments to rule out other options and eventually find my answer when I noticed the two projects each had the same identically-named .xaml file within a single solution.


Abstract example

1 solution which has 2 projects: PRINTNOTESPROJ and MAKENOTESPROJ.

During development, I used a XAML window in MAKENOTESPROJ which should actually be part of PRINTNOTESPROJ in the Release executables.

Upon switching to Build->Release, I simply clicked-down on "PrintWindow.XAML" (in MAKENOTESPROJ & with child PrintWindow.xaml.cs) and DRAGGED it onto my PRINTNOTESPROJ.

*This is the key. I forgot that doing this only COPIES the related files to the new project [at least in VS 2010]. From this point onward I received your error until I DELETED "PrintWindow.xaml" from my original project MAKENOTESPROJ.

I hope this makes sense... Please comment with questions!