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

后端 未结 5 1444
南笙
南笙 2021-02-04 23:50

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

Library project file cannot specify ApplicationDefinition element.

相关标签:
5条回答
  • 2021-02-04 23:56

    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).

    0 讨论(0)
  • 2021-02-05 00:02

    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.

    0 讨论(0)
  • 2021-02-05 00:06

    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!

    0 讨论(0)
  • 2021-02-05 00:14

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

    Change the Build Action Property of that image to resources

    0 讨论(0)
  • 2021-02-05 00:18

    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).

    0 讨论(0)
提交回复
热议问题