none
using a wpf window in a class library project

    Question

  • Is it possible to use window (namespace System.Windows) in a library project?

    I've tried to use some wpf controls in a library project, but the only control I can add is the UserControl.xaml (and I need to show some windows programmatically).

    I've also tried to copy-paste a window into the dll project but I received the error:

     

    Library project file cannot specify ApplicationDefinition element.

     

     

    thanks

    GHillo

    Sunday, January 27, 2008 8:16 PM

Answers

  • Hi, GHillo,

     

    Please consider creating WPF User Control Library or WPF Custom Control Library instead of general class library. You will then be able to add a Window (and other WPF-specific elements). And it can certainly contain general-purpose classes. And it will be built into an assembly no different from that from general class library project.

     

    Of couse, nothing should prevent you from adding a Window to general class library project. VS just does not expose Window from add new items dialog. A workaround is to add a User Control Item, and then change it to derive from Window.

     

    We will keep your experience for review in future releases. Thanks.

     

    Zhanbo

     

    Thursday, January 31, 2008 5:48 AM
  • It should be possible since a WPF library is just another .net lib.  You probably need some extra info in your project.  One way you can possibly  find out is to make a diff of an empty library and WPF control lib project (including the project file itself).
    My guess though is that it would be easier to simply create another WPF custom control library where you create the form you need.  You can than use this lib in your other lib.
    Monday, January 28, 2008 9:03 AM
  • Bragi already gives a good point on this topic, and I want to point out that "ApplicationDefinition" is not supported in library project, try using "Page" instead.

    Hope this helps
    Tuesday, January 29, 2008 3:17 AM

All replies

  • It should be possible since a WPF library is just another .net lib.  You probably need some extra info in your project.  One way you can possibly  find out is to make a diff of an empty library and WPF control lib project (including the project file itself).
    My guess though is that it would be easier to simply create another WPF custom control library where you create the form you need.  You can than use this lib in your other lib.
    Monday, January 28, 2008 9:03 AM
  • Bragi already gives a good point on this topic, and I want to point out that "ApplicationDefinition" is not supported in library project, try using "Page" instead.

    Hope this helps
    Tuesday, January 29, 2008 3:17 AM
  • Hi, GHillo,

     

    Please consider creating WPF User Control Library or WPF Custom Control Library instead of general class library. You will then be able to add a Window (and other WPF-specific elements). And it can certainly contain general-purpose classes. And it will be built into an assembly no different from that from general class library project.

     

    Of couse, nothing should prevent you from adding a Window to general class library project. VS just does not expose Window from add new items dialog. A workaround is to add a User Control Item, and then change it to derive from Window.

     

    We will keep your experience for review in future releases. Thanks.

     

    Zhanbo

     

    Thursday, January 31, 2008 5:48 AM
  • Hi all,
    You can get rid of the error specified simply by removing App.xaml file from the library project.
    Greetings,
    Emrah


    Sunday, February 17, 2008 1:28 PM
  • The problem for me was having a file in a class library which Build Action is ApplicationDefinition.  Normally that is App.xaml but I have had that happen to xaml files that I simply copied from one assembly to another.  

    So if you do not have an App.xaml in your assembly scan through the properties of all the files, or maybe the recently added ones will be quicker, and verify that their build action is not ApplicationDefinition.

    Adk
    • Proposed as answer by Adriandk Thursday, September 04, 2008 3:47 PM
    Thursday, September 04, 2008 3:46 PM
  • Just clarification to AdrianK's answer:

    Problem happens when you drag and drop files in Visual Studio from a WPF project to non-WPF. VS assigns to *.xaml files a wrong Build Action.

    To solve: Click on *.xaml files you've just dragged and in Properties window in the line Build Action change ApplicationDefinition to Page.
    It worked for me.
    Friday, May 15, 2009 10:23 PM
  • Thank you very much! this is a big help for me!

    Thursday, August 06, 2009 8:31 AM
  • Thank you very much! this is a big help for me to!
    Thursday, March 11, 2010 9:31 AM
  • farfareast has solved my problem for me.  Thanks, the build action is the issue.  Much of the other accepted answers are not really very to the point!
    Thursday, July 15, 2010 10:35 AM
  • This can happen when you create a project as WPF application and tries to convert it to a dll. To resolve the problem open your .csProj in a note pad. Try to look for an element like

    <ApplicationDefition...

    ......

    </ApplicationDefinition>

     

    Remove this whole element. This will resolve the problem. If you have something defined like ResourceDictionary or UI code in App.Xaml then you need to move this code somewhere else.


    Marquette uni
    Friday, December 17, 2010 9:11 PM
  • Bragi already gives a good point on this topic, and I want to point out that "ApplicationDefinition" is not supported in library project, try using "Page" instead.

    Hope this helps
     

    I got this error after merging assemblies.  As Marco points out, the real problem was the build action.  It was reset to "ApplicationDefinition".  Setting all of my moved xaml files to the setting below resolved the issue for me.  

    • Build Action: Page
    • Copy to Output Directory: Do not copy
    • Custom Tool: MSBuild:Compile

    The unmoved xaml files retained the correct settings.  

    Thanks Marco!

     

    Tuesday, April 05, 2011 11:04 PM
  • This is great news now I just need to work out how to make Prism work. Anyone know how to run a Prism bootstraper this way???
    Wednesday, November 16, 2011 12:48 AM
  • Add a UserControl (WPF) then convert it to a Windows base by simply changing UserControl to Window in the XAML and CodeBehind - then VS will add everything else for you (you might need to add reference to System.Xaml.dll)
    • Proposed as answer by hamsterlegs21 Sunday, April 28, 2013 11:17 AM
    Sunday, April 28, 2013 11:17 AM
  • Very nice solution. Thank you for this info.

    Kamil Zień

    Monday, July 21, 2014 9:05 AM
  • Awsome! Thank you!
    Tuesday, November 01, 2016 12:28 AM