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

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.
Posted
Updated 5-Aug-20 9:12am
Comments
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
XML
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style x:Key="TextBlockWithMargin" TargetType="TextBlock">
        <Setter Property="Margin" Value="10" />
    </Style>


User control
XML
<UserControl x:Class="x.x.Test"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">
    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/ProjectA;component/MyResourceDictionary.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </UserControl.Resources>
    <Grid x:Name="LayoutRoot" removed="White">

    </Grid>
</UserControl>


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.

thanks
 
Share this answer
 
v3
Comments
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)



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