Click here to Skip to main content
15,906,816 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

Can any help me on creating a wpf window in a class library project.
Please share a sample code.
Posted
Comments
Pheonyx 28-Nov-14 3:22am    
You create the window in the same way you would with a WPF application, you just need to make sure you have all the references set up.

What exactly is your issue?

1 solution

If you start building your class library on a green field I would suggest this:
1. Start with a new WPF Application project
2. Add your UI components as you usually do
3. In Solution Expoler right click on the project and choose Properties
4. On Application tab change Output type from Windows Application to Class Library
5. To get rid of compilation errors delete the App.xaml file from your project

After all this, when you build this particular project you will get a DLL, that can be used in other projects.

If you already have some Class Library project, than you can add references to libraries that are needed for WPF, as Pheonyx mentioned in their comment, but Visual Studio can do it by itself.
1. Right click on your Class Library project
2. Choose Add > New Item
3. In the left bar from Visual C# items select WPF > User Control (WPF)
4. To use XAML add reference to Windows.XAML assembly (Project > right click on References > Add Reference > from Assemblies add Windows.XAML )

In the project, where you want use the library:
1. Add the output DLL to References / or just reference your Library project
2. Considering that the namespace of your library is ClassLibrary1 your can use its components in XAML with this code:
<Window  
...
xmlns:lib="clr-namespace:ClassLibrary1;assembly=ClassLibrary1"
...
>
<lib:UserControl1 /> <!-- User Control from the library -->
</Window>
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900