Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set up application resources from code

Tags:

c#

resources

wpf

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.

like image 337
user589195 Avatar asked Mar 16 '12 14:03

user589195


People also ask

What is application resources?

Resources are the additional files and static content that your code uses, such as bitmaps, layout definitions, user interface strings, animation instructions, and more. You should always externalize app resources such as images and strings from your code, so that you can maintain them independently.

How do you create a resource dictionary?

Tip You can create a resource dictionary file in Microsoft Visual Studio by using the Add > New Item… > Resource Dictionary option from the Project menu.

How do I add a resource to a project in WPF?

To add a Resource Dictionary into your WPF application, right click the WPF project > add a Resource Dictionary. Now apply the resource "myAnotherBackgroundColor" to button background and observe the changes.

What is ResourceDictionary?

A resource dictionary is a repository for XAML resources, such as styles, that your app uses. You define the resources in XAML and can then retrieve them in XAML using the {StaticResource} markup extension and {ThemeResource} markup extension s. You can also access resources with code, but that is less common.


2 Answers

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); 
like image 165
NestorArturo Avatar answered Sep 17 '22 21:09

NestorArturo


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) }); 
like image 21
Guillaume Avatar answered Sep 20 '22 21:09

Guillaume