28

I have a c# project that was a WPF application but I now want to build it as a dll. I have previously done this by removing the app.xaml from the project and setting its build type to dll.

The issue I have now is that the app.xaml contains some xaml to instantiate the application variables. To get round this I am trying to programmatically set these application variables from within the first xaml window that will be called.

The xaml I am trying to emulate in code is:

<Application.Resources>
    <ResourceDictionary>
      <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="Resources/Styles/Shared.xaml"/>
        <ResourceDictionary Source="Resources/Styles/ToolBar.xaml"/>
        <ResourceDictionary Source="Resources/Styles/GroupBox.xaml"/>
        <ResourceDictionary Source="Resources/Styles/ZoomBox.xaml"/>
        <ResourceDictionary Source="Resources/Styles/ScrollBar.xaml"/>
        <ResourceDictionary Source="Resources/Styles/Expander.xaml"/>
        <ResourceDictionary Source="Resources/ApplicationToolbar.xaml"/>
        <ResourceDictionary Source="Resources/DesignerItem.xaml"/>
        <ResourceDictionary Source="Resources/Styles/ToolboxItem.xaml"/>
        <ResourceDictionary Source="Resources/Styles/Toolbox.xaml"/>
        <ResourceDictionary Source="Resources/Connection.xaml"/>
        <ResourceDictionary Source="Resources/Slider.xaml"/>
        <ResourceDictionary Source="Resources/ScrollViewer.xaml"/>
        <ResourceDictionary Source="Resources/StatusBar.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
  </Application.Resources>

This is the code I have:

ResourceDictionary myResourceDictionary = new ResourceDictionary();
            myResourceDictionary.Source = new Uri("C:\\Resources\\Styles\\Shared.xaml");
            Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);
            myResourceDictionary.Source = new Uri("C:\\Resources\\Styles\\ToolBar.xaml");
            Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);
            myResourceDictionary.Source = new Uri("C:\\Resources\\Styles\\GroupBox.xaml");
            Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);
            myResourceDictionary.Source = new Uri("C:\\Resources\\Styles\\ZoomBox.xaml");
            Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);
            myResourceDictionary.Source = new Uri("C:\\Resources\\Styles\\ScrollBar.xaml");
            Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);
            myResourceDictionary.Source = new Uri("C:\\Resources\\Styles\\Expander.xaml");
            Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);
            myResourceDictionary.Source = new Uri("C:\\Resources\\ApplicationToolbar.xaml");
            Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);
            myResourceDictionary.Source = new Uri("C:\\Resources\\DesignerItem.xaml");
            Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);
            myResourceDictionary.Source = new Uri("C:\\Resources\\Styles\\ToolboxItem.xaml");
            Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);
            myResourceDictionary.Source = new Uri("C:\\Resources\\Styles\\Toolbox.xaml");
            Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);
            myResourceDictionary.Source = new Uri("C:\\Resources\\Connection.xaml");
            Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);
            myResourceDictionary.Source = new Uri("C:\\Resources\\Slider.xaml");
            Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);
            myResourceDictionary.Source = new Uri("C:\\Resources\\ScrollViewer.xaml");
            Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);
            myResourceDictionary.Source = new Uri("C:\\Resources\\StatusBar.xaml");
            Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);

Should this work?

I'm hitting a problem in that Toolbar.xaml references a resource declared in Shared.xaml but its not getting picked up and im getting the following error.

Cannot find resource named 'ToolbarSelectedBackgroundBrush'. Resource names are case sensitive.

Here is where the resource is delcared in shared.xaml

<LinearGradientBrush x:Key="ToolbarSelectedBackgroundBrush" StartPoint="0,0" EndPoint="0,1">
    <GradientBrush.GradientStops>
      <GradientStopCollection>
        <GradientStop Color="#FFFEE3" Offset="0.0"/>
        <GradientStop Color="#FFE797" Offset="0.4"/>
        <GradientStop Color="#FFD750" Offset="0.4"/>
        <GradientStop Color="#FFE796" Offset="1.0"/>
      </GradientStopCollection>
    </GradientBrush.GradientStops>
  </LinearGradientBrush>

and here's where its referenced in toolbar.xaml

<Setter TargetName="Border" Property="Background" Value="{StaticResource ToolbarSelectedBackgroundBrush}" />

Sorry for the essay of a question but thought id provide as much info as I could. Let me know if you need anything else.

Ahmad
  • 1,462
  • 15
  • 23
user589195
  • 4,180
  • 13
  • 53
  • 81
  • Your question asks about writing code, because you no longer use `App.xaml` and want to write code as a workaround. Turns out you *can* still use `App.xaml` as of https://stackoverflow.com/questions/4441227/wpf-class-library-with-resource-dictionary/4441500#4441500 . Then the caller DLL instanciates `App` like in https://stackoverflow.com/a/26890426/1429390 . Works for me with all resources working, design time and run time thanks to relative URIs. – Stéphane Gourichon Sep 20 '17 at 13:31

3 Answers3

34

This code works for me. I just changed the URIs to relative:

ResourceDictionary myResourceDictionary = new ResourceDictionary();

myResourceDictionary.Source = new Uri("Dictionary1.xaml", UriKind.Relative);
Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);

myResourceDictionary.Source = new Uri("Dictionary2.xaml", UriKind.Relative);
Application.Current.Resources.MergedDictionaries.Add(myResourceDictionary);
Kay Zed
  • 1,304
  • 2
  • 21
  • 31
NestorArturo
  • 2,476
  • 1
  • 18
  • 21
12

I think you need to specified the name of the component were the resource is sitting in

<ResourceDictionary Source="/<YourDllName>;component/Resources/Styles/Shared.xaml" />

If your dll is named My.Wpf.Component.dll you should put My.Wpf.Component

so in code it should be

Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary { Source = new Uri(@"/<YourDllName>;component/Resources/Styles/Shared.xaml", UriKind.Relative) });
Guillaume
  • 1,176
  • 2
  • 11
  • 27
0

You have to create separate ResourceDictionary file e.g. Style.xaml that contains (don't forget namespaces)

<ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="Resources/Styles/Shared.xaml"/>
    <ResourceDictionary Source="Resources/Styles/ToolBar.xaml"/>
    <ResourceDictionary Source="Resources/Styles/GroupBox.xaml"/>
    <ResourceDictionary Source="Resources/Styles/ZoomBox.xaml"/>
    <ResourceDictionary Source="Resources/Styles/ScrollBar.xaml"/>
    <ResourceDictionary Source="Resources/Styles/Expander.xaml"/>
    <ResourceDictionary Source="Resources/ApplicationToolbar.xaml"/>
    <ResourceDictionary Source="Resources/DesignerItem.xaml"/>
    <ResourceDictionary Source="Resources/Styles/ToolboxItem.xaml"/>
    <ResourceDictionary Source="Resources/Styles/Toolbox.xaml"/>
    <ResourceDictionary Source="Resources/Connection.xaml"/>
    <ResourceDictionary Source="Resources/Slider.xaml"/>
    <ResourceDictionary Source="Resources/ScrollViewer.xaml"/>
    <ResourceDictionary Source="Resources/StatusBar.xaml"/>
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

end reference it in all of yours controls

pamidur
  • 572
  • 4
  • 11