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

I have small problem with WPF app as a library. It was originally designed to be standalone application but later we decided to use it as a plugin. I searched in Google and now I know that I should use WPF Custom Control Library or WPF User Control Library. My project is WPF App, and after changing output type to class library VS gives me following warnings:

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

Google told me to change App.xaml build options to page and here I get another problem. I am using external style and after start I get exceptions in every place with "{StaticResource Foreground}". I am fresh in WPF so I will appreciate any help.
Updated 5-Aug-20 9:12am
Pheonyx 4-Apr-13 4:03am    
My advice would be to start with a blank project and then copy the WPF Windows/Controls/Classes etc over to the new project. I find that is easier than trying to change a project type once you have made it.
Member 12395765 4-Mar-19 12:46pm    
huge thanks !

1 solution

You should not have the app.xaml in a user control lib. get rid of that.

The problem you have is that the user control lib is really for UIelements that are effectively told what resources to use by the application that references the user control lib. The way around this is to create a resource dictionary with the resources in it (and or references) and then on the user controls reference this resource in the usercontrol.resources section.

i.e.(note this is from a Silverlight Project but principals are the same)

Resource Dictionary
<ResourceDictionary xmlns=""
    <Style x:Key="TextBlockWithMargin" TargetType="TextBlock">
        <Setter Property="Margin" Value="10" />

User control
<UserControl x:Class="x.x.Test"
    d:DesignHeight="300" d:DesignWidth="400">
                <ResourceDictionary Source="/ProjectA;component/MyResourceDictionary.xaml" />
    <Grid x:Name="LayoutRoot" removed="White">


You would normally have the resources set in the application app.xaml. You have mentioned that the application has been changed to a usercontrol lib. So where will this be referenced? Are you saying that you want to run a usercontrol lib on its own?

To get around the compiletime errors you can use {DynamicResource Foreground}. Dynamic resource will not get set until runtime so compilations should work. You will still have issues if the usercontrol is not aware or cannot find the correct resource though.

If you provide a bit more background to what you are doing or want to achieve then I can probably help further.

Share this answer
Tapirro 4-Apr-13 5:20am    
I have copied your part from user control to my main xaml window, using my resource dictionary source (taken from app.xaml). As you said I also delete App.xaml file and now everything is working perfectly. Thx!

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

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