Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WPF VisualStudio2012
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 3-Apr-13 23:00pm
Tapirro354
Comments
Pheonyx at 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.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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="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
<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
  Permalink  
v3
Comments
Tapirro at 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
0 OriginalGriff 304
1 Sergey Alexandrovich Kryukov 255
2 Shweta N Mishra 216
3 Maciej Los 210
4 PIEBALDconsult 184
0 OriginalGriff 7,630
1 Sergey Alexandrovich Kryukov 7,022
2 DamithSL 5,586
3 Manas Bhardwaj 4,946
4 Maciej Los 4,525


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 4 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100