Using App.xaml in .dll-assembly

I'm currently working on a wpf-application cooperating with another company. We have only part of the sourcecode and therefore only compile a few .dll's, the main executable however get's shipped to us as finished product.

The application uses style-files that get loaded at startup by the main application from files in a specific folder, therefore we faced a the problem that these styles don't get applied at design-time, but only at runtime.

The Problem:

While changing tons of stuff for a new UI we are working on I started using a new user-control (which derrives from button) in order to have a consistant layout all over the app. The default style sadly had the height set to "auto", which caused the layouts to be very weired during design-time, but work at runtime (because of the applied style which sets the height to the desired value). Fixing this was a priority as our design-team struggled with the messed up designs.

To fix this i had tree possibilities - eighter set the correct height for each button manually (which the design-team started to do), include a style at each control we're editing, or load resources in an App.xaml. Hardcoding the height is neighter elegant nor usable as i suspected the size of the buttons to be changed at a later point (and i was proven right later on).

I decided to go for the last approach as this seemed to be the easyest and fastest. However - we're not compiling the main executable, only a .dll (which does not have a app.xaml where you can define the styles).

The Solution

Seaching the web i found a working solution at The team blog of the Expression Blend and Design products. The team blog of the Expression Blend and Design products. That page suggests that using App.xaml is possible for .dll-files at designtime (using them only during design-time, and excluding them at compile-time). To get this working well for us i had to make a few changes as we don't use one style-file but have the styles and themes separated in many files and load them as resources, using it as shown in the example would have caused lots of issues when adding new files. however the principle is quite easy and easily understandable:

open the .csproj of your project and insert the following:

  <ItemGroup Condition="'$(DesignTime)'=='true' AND
              '$(BuildingInsideVisualStudio)'!='true'
               AND '$(BuildingInsideExpressionBlend)'!='true'">
    <Resource Include="Skins\DesignTimeTheme.xaml">
      <Generator>MSBuild:Compile</Generator>
      <SubType>Designer</SubType>
    </Resource>
    <Compile Include="App.xaml.cs">
      <DependentUpon>App.xaml</DependentUpon>
      <DesignTime>true</DesignTime>
    </Compile>
  </ItemGroup>

Explanation:

Adding the condition to the ItemGroup applies that to all resources that lie below. That means, i have the App.xaml and the Theme there only at design-time, but they'll be excluded at runtime. The advantage of this is that upon release, the resources are not there several times (once for the application and once for the library).

Now, every new style-file is added into this ItemGroup.

Summary:

Using the above, i was able to load my Styles in app.xaml (as you'd usually do with a wpf-project) - but doing this for the assembly (without creating a fake executable or so).

The solution may not be perfect, however it prooved to be quite useful to our designers which had to modify some styles.

Matthias

Read more posts by this author.