locked
Separate resources into another project? RRS feed

  • Question

  • Is there a way to keep all of one's resource dictionaries in a separate project (i.e. not in the main application where all the windows, controls, etc. are) and still have the controls, etc. use those styles?  Basically my project is getting big and is significantly slowing Blend down for some reason when I try to do things like edit styles and templates (is it because of using DynamicResource vs StaticResource?).  I'm trying to figure out the best way to split up my project so that everything isn't contained in one big project.  Any suggestions would be helpful, thanks.
    Friday, July 20, 2007 4:00 PM

Answers

  • Hi,

    Of course.

    In your first project/assembly:
    App.xaml
    Code Snippet

    <Application
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        x:Class="Temp.App"
        StartupUri="Window1.xaml">
        <Application.Resources>
        <ResourceDictionary>
          <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/WPFResourcesLib;component/TestDictionary.xaml"/>
          </ResourceDictionary.MergedDictionaries>
          </ResourceDictionary>
        </Application.Resources>
    </Application>


    Window.xaml
    Code Snippet

    <Window
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        x:Class="Sample.Window1"
        Height="300" Width="300" WindowStartupLocation="CenterScreen"
        >
      <Grid>
        <Button Content="MyStyle" HorizontalAlignment="Center" VerticalAlignment="Center" Background="{DynamicResource Brush1}"/>
      </Grid>
    </Window>


    Now in your second assembly/project, the ResourceDictionary:
    Code Snippet

    <ResourceDictionary
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        <LinearGradientBrush x:Key="Brush1" EndPoint="1,0.5" StartPoint="0,0.5">
            <GradientStop Color="#FFB77CDF" Offset="0"/>
            <GradientStop Color="#FFB0E284" Offset="1"/>
        </LinearGradientBrush>
    </ResourceDictionary>


    Then, there are 2 important things to do, in your first project, add a reference to your resources assembly. Second, set the Build Action of TestDictionary.xaml to Resource (NOT embedded resource). Then, it will work.

    Best regards,

    Benny
    Thursday, July 26, 2007 12:10 PM

All replies

  • Yes, you can. Try this:

    <Application.Resources>

      <ResourceDictionary>

        <ResourceDictionary.MergedDictionaries>

          <ResourceDictionary Source="pack://application:,,,/ReferencedAssembly;component/Subfolder/Your Dictionary.xaml"/>

        </ResourceDictionary.MergedDictionaries>

      </ResourceDictionary>

    </Application.Resources>

    Monday, July 23, 2007 9:13 AM
  • No luck.  I made a new library project (called WPFResourcesLib), added a resource dictionary to it called "TestDictionary.xaml", and added this to my main project's App.xaml:

     

    Code Snippet

     

    <ResourceDictionary>

    <ResourceDictionary.MergedDictionaries>

    <ResourceDictionary Source="pack://application:,,,/WPFResourcesLib;component/TestDictionary.xaml"/>

    ...

    ...

    </ResourceDictionary.MergedDictionaries>

    </ResourceDictionary>

     

    It gives me the lovely:

     

    'pack://application:,,,/WPFResourcesLib;component/TestDictionary.xaml' value cannot be assigned to property 'Source' of object 'System.Windows.ResourceDictionary'. Cannot locate resource 'testdictionary.xaml'.  Error at object 'System.Windows.ResourceDictionary' in markup file 'UntitledProject1;component/app.xaml' Line 28 Position 29. 

     

    And yes, I added a reference to the project...

     

    I read the documentation on this stuff and this should work!  There is something fundamentally wrong here...

    Monday, July 23, 2007 7:40 PM
  • Does anyone have a simple example of this syntax working between projects?

    Thursday, July 26, 2007 11:46 AM
  • This works for me every time:

    Code Snippet

    <ResourceDictionary.MergedDictionaries>

    <ResourceDictionary Source="/WPFResourcesLib;component/TestDictionary.xaml" />

    </ResourceDictionary.MergedDictionaries>

    • Proposed as answer by miridfd Tuesday, December 8, 2009 5:33 PM
    Thursday, July 26, 2007 12:02 PM
  • Hi,

    Of course.

    In your first project/assembly:
    App.xaml
    Code Snippet

    <Application
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        x:Class="Temp.App"
        StartupUri="Window1.xaml">
        <Application.Resources>
        <ResourceDictionary>
          <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/WPFResourcesLib;component/TestDictionary.xaml"/>
          </ResourceDictionary.MergedDictionaries>
          </ResourceDictionary>
        </Application.Resources>
    </Application>


    Window.xaml
    Code Snippet

    <Window
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        x:Class="Sample.Window1"
        Height="300" Width="300" WindowStartupLocation="CenterScreen"
        >
      <Grid>
        <Button Content="MyStyle" HorizontalAlignment="Center" VerticalAlignment="Center" Background="{DynamicResource Brush1}"/>
      </Grid>
    </Window>


    Now in your second assembly/project, the ResourceDictionary:
    Code Snippet

    <ResourceDictionary
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        <LinearGradientBrush x:Key="Brush1" EndPoint="1,0.5" StartPoint="0,0.5">
            <GradientStop Color="#FFB77CDF" Offset="0"/>
            <GradientStop Color="#FFB0E284" Offset="1"/>
        </LinearGradientBrush>
    </ResourceDictionary>


    Then, there are 2 important things to do, in your first project, add a reference to your resources assembly. Second, set the Build Action of TestDictionary.xaml to Resource (NOT embedded resource). Then, it will work.

    Best regards,

    Benny
    Thursday, July 26, 2007 12:10 PM
  • The only options for "Build Action" I see in the drop down are:

     

    None

    Compile

    Content

    Embedded Resource

    Page

    Component

     

    I tried each option... still crashes.  "Can't find TestDictionary.xaml", etc.

     

    Did the steps you said.  First thing I did was right click my solution, add new project, chose "Class Library", added the "TestDictionary.xaml", and wrote the code for the ResourceDictionary.  Did I do anything wrong as far as creating the project and file?  Is there any other step or requirement I am missing?

    Thursday, July 26, 2007 1:30 PM
  • Hi,

    Create a new project starting from a .NET 3.0 project, then it will be there.

    Best regards,

    Benny
    Thursday, July 26, 2007 1:37 PM
  • Got it!

    Thursday, July 26, 2007 1:43 PM