Have default string resource file and override it with custom ones - c#

So the title is perhaps not completly clear.
I have a Strings.xaml file which contains several strings which are used in the application.
Strings.xaml
<!-- GENERAL FOR ALL TESTS -->
<my:String x:Key="AppTitle">AppName</my:String>
<my:String x:Key="TestName1">test_1</my:String>
<my:String x:Key="TestName2">test_2</my:String>
<!-- DEFAULT MESSAGES -->
<my:String x:Key="TestMessage">This is a default message</my:String>
<my:String x:Key="TestDescription">This is a default description</my:String>
<my:String x:Key="OnlyCustomInTest2">This string is used as a default message if not overridden by custom resource file</my:String>
</ResourceDictionary>
This resource file works great. What I'm wondering is if there is any built in way that I can use Strings.xaml as a default resource file and then override specific strings that are custom for different program modes? Like having Strings.xaml default and use Test_1_Strings.xaml and Test_2_Strings.xaml to override some strings for custom messages.
Test_1_Strings.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my="clr-namespace:System;assembly=mscorlib">
<!-- CUSTOM FOR TEST 1 -->
<my:String x:Key="TestMessage">This is a message for test 1</my:String>
<my:String x:Key="TestDescription">This is a description for test 2</my:String>
</ResourceDictionary>
Test_2_Strings.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my="clr-namespace:System;assembly=mscorlib">
<!-- CUSTOM FOR TEST 2 -->
<my:String x:Key="TestMessage">This is a message for test 2</my:String>
<my:String x:Key="TestDescription">This is a description for test 2</my:String>
<my:String x:Key="OnlyCustomInTest2">This is the overridden message for test 2</my:String>
</ResourceDictionary>
The reason I want to do this is because I have many different program modes where most of the resources are the same but some are custom. Instead of having to change a shared entry in 8 different resource files I could do it in only one place.

Resource lookup in WPF traverses from bottom to top i.e. any resource usage will first look for resource in its parent container which can be Grid, StackPanel etc. If not found in parent container will look for resource in parent's parent container and so on to UserControl, Window till it reach App resources.
Also, any resource later defined under resources section overrides the resource added previously with same key. This is true for resources defined under different resource dictionaries but not within the same XAML file. If you try to declare two items with same key, it will fail with key already exists exception.
You can take advantage of above stated features to your use.
Assuming you are merging the resources under App resources, what you can do is add Strings.xaml at top and then add other resource dictionaries Test_1_Strings.xaml and Test_2_Strings.xaml. This way resources with same name will be overridden and resource defined at last will always be resolved.
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Strings.xaml"/>
<ResourceDictionary Source="Test_1_Strings.xaml"/>
<ResourceDictionary Source="Test_2_Strings.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
So when you declare TextBlock to refer to StaticResource TestMessage.
<TextBlock Text="{StaticResource TestMessage}"/>
it will print This is a message for test 2.
If you change the order and add Test_1 after Test_2, textBlock Text will be - This is a message for test 1.

Related

Unable to Resolve Resources

I am using the MaterialDesign for XAML package in WPF. When I run my application, all styles and controls are rendered as expected. However in the XAML designer I have dozes of errors such as "The resource 'MaterialDesignFlatButton' could not be resolved." Example of a line that is throwing that error:
<Button Style="{StaticResource MaterialDesignFlatButton}" IsDefault="True" Margin="0 8 8 0" ...
My app.xaml contents is as follows:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.Blue.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.LightBlue.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
I have attempted the top solution proposed on The resource "x" could not be resolved. but that causes the project to fail to run (I believe I am not using the correct pathing when attempting to use the proposed "absolute pack URI"). So I have two questions at this point:
Is there a reason the resources would fail to resolve in the XAML designer given the way I have defined them in App.xaml (per the developer guide: https://github.com/MaterialDesignInXAML/MaterialDesignInXamlToolkit/wiki/Getting-Started)?
How can I find out the "absolute pack URI" to use for the source for my resource dictionaries?
In the past, I had problems like this.
The error causes are as follow.
1. Setup and setting config
About this, please check the github and material design homepage.
2. Build and Compiler problem
About this, users may set the "Platform Target" as "x64".
That can invoke errors because material designer tool use "x32" compiler, so please use "any cpu" or "x32".
I had this problem with the flat accent button, while every other button style worked. I added the resource for buttons, then the error was gone. Then I removed the button resource... and the error was still gone.
https://github.com/MaterialDesignInXAML/MaterialDesignInXamlToolkit/wiki/FAQ
Exception: Cannot find resource named 'MaterialDesign...'
This error typically comes when you have a static resource referencing
one of the material design styles, and have not included the
appropriate resource dictionary that contains the style. Try the
following:
Ensure that you have loaded all of the default material design styles in your App.xaml. You can find directions for this in the
Getting Started guide.
Ensure you have referenced the control specific resource dictionary that contains the style. The path for this is resource dictionary
should be .xaml" />. For example, if you were trying to reference the
MaterialDesignFloatingActionMiniButton style for a button, the
resource dictionary source would be: . Typically these inclusions are done at root of your Window, User
Control, or Template. You can find the full list of the resource
dictionaries here

Specify or modify class library style resource values from referencing add-ins

I have a c# wpf-based class library with models, view models, and views, which I am using in multiple add-ins for dektop applications that have APIs which support hosted dockable WPF user controls. From here I can use different controls and launch dialogs to interact with various data structures in the 3rd-party application. My class library doesn't have an App.xaml file.
I have created a ResourceDictionary xaml file that I am using as a main style sheet throughout my views. In my user controls and windows, I reference it:
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="../Resources/styleMain.xaml"/>
</ResourceDictionary.MergedDictionaries
Inside of this style sheet, I manage my various controls to simplify and unify my views. At the lowest level, I have settings for things like font size, font family, and color. For example, I have several brushes that I use throughout my controls. One looks like this:
<Color x:Key="ColorBase_Primary" R="255" G="0" B="115" A="255"/>
<SolidColorBrush x:Key="Color_Primary" Color="{StaticResource ColorBase_Primary}"/>
This all works great for design and testing. However, I am referencing this class library in multiple other projects. In these other projects, I am hosting the main user control from my class library:
<UserControl x:Class="MyAppHost.ucDockingHost"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:MyAppHost"
xmlns:viewModels="clr-namespace:MyClassLibrary.ViewModels;assembly=MyClassLibrary"
xmlns:views="clr-namespace:MyClassLibrary.Views;assembly=MyClassLibrary"
mc:Ignorable="d">
<UserControl.DataContext>
<viewModels:vmMain/>
</UserControl.DataContext>
<Grid>
<views:ucMainGrid ViewModelMain="{Binding}" Property1="SomeValue" Property2="SomeOtherValue"/>
</Grid>
</UserControl>
Here I am setting DependencyProperties from the add-in application that references my class library without any problems, and all of my bindings and control behaviors are working great.
What I am now hoping to do is be able to specify from my add-in (and not the main class library) the basic resource values that are used throughout my style sheet, especially color (as in being able to change the value associated with x:Key="ColorBase_Primary"). This way, I can adapt different implementations to the branding of different clients, etc. I have been searching for a long time for any examples or insight and keep coming up short.
It would be easy enough to simply deploy the entire class library independently with each application and adjust the style sheet directly, but that doesn't seem right at all. Is there some way for me to create a ResourceDictionary in my add-in applications that could supply, modify or and/override the resource values specified in my class library? Or should I do it through a DependencyProperty in my view model? That doesn't seem right either.
Do not use the MergedDictionaries in your library, i.e. remove this:
In your application, you then merge all resource dictionaries from your library in the correct order:
<Application x:Class="WpfApplication1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/WpfControlLibrary1;component/Brushes.xaml"/>
<ResourceDictionary Source="pack://application:,,,/WpfControlLibrary1;component/Controls.xaml"/>
</ResourceDictionary.MergedDictionaries>
<SolidColorBrush x:Key="brush">Red</SolidColorBrush>
</ResourceDictionary>
</Application.Resources>
</Application>
You can then simply override any resource you want by defining another resource with the same x:Key like I have done with the SolidColorBrush above. This resource overrides any resource with an x:Key of "brush" defined in either Brushes.xaml or Controls.xaml.

Use a different DataTemplate with the same key if called from different exe

In a wpf.dll, I define a DataTemplate with a key MorphControl in ResourceDictionary XAML:
<ResourceDictionary>
<DataTemplate x:Key="MorphControl">
<n1:m1 />
</DataTemplate>
</ResourceDictionary>
If this wpf.dll is called by an exe ( say, A.exe), then I would like the above FindResource("MorphControl") to return m1 related resrouce.
But, there is a possibility that wpf.dll is being called by another exe ( say, B.exe), in B.exe, the same key is being redefined to use another value, ie, inside B.exe, this definition exists:
<ResourceDictionary>
<DataTemplate x:Key="MorphControl">
<n2:m2 />
</DataTemplate>
</ResourceDictionary>
So when B.exe is calling, I want FindResource["MorphControl"] to return m2 related resource.
I want to use the same wpf.dll for both A.exe and B.exe. And I don't want to move the MorphControl definition from wpf.dll to A.exe-- it has to remain at wpf.dll.
Also, this particular Resource key must remain the same throughout because inside wpf.dll, I have a DataTemplateSelector logic that I don't want to touch
How to do this, or is this possible?
Here is one unorthodox way that should work (I didn't try it, but based on some other things I did, it should work):
Create two ResourceDictionaries, one for each exe in two separate files.
Create class that will inherit ResourceDictionary, lets call it ExeAwareDictionary. It should have to private fields/properties of type ResourceDictionary and you should override item[object] property.
When getter for item property is called, you can check which exe it is (by checking current process) and return resource with given key from appropriate "inner" dictionary.
You might want to also override some other properties like Keys and Values, etc.
I found my answer ( same one as this answer): just define the ResourceDictionary.MergeDictionaries with wpf.dll DataTemplate first, follow by B.exe DataTemplate. In other words:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="wpf.dll.xaml"/>
<ResourceDictionary Source="B.exe.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>

ResourceDictionary.MergedDictionaries causes weird errors

Inside my WPF Application I am including a ResourceDictionary from another Project.
<Application x:Class="namespace.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!-- This Line causes an Error -->
<ResourceDictionary Source="pack://application:,,,/Commons;Component/Generic.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
Metadata override and base metadata must be of the same type or derived type.
The solution builds successful and runs.
Restarting Visual Studio doesn't fix it.
Cut and Paste the <ResourceDictionary Source="..." /> line causes another error as explained here in the Comments:
Value Cannot be Null. Parameter Name: item. Restarting Visual Studio will then bring back the old error.
Sadly I haven't found out how to reproduce this error, I can only tell you something more about the environment im using:
Visual Studio 2015 Professional, Version 14.0.25431.01 Update 3
And allthough I doubt, those are associated with my problem, here my installed Plugins:
Resharper Ultimate 2017.1.1
GitExtensions Version 2.49.03
Sinatr's comment hinted me to read more about theming.
ThemeInfo
Inside of a Custom Control Library theres automatically created a ThemeInfoAttribute inside AssemblyInfo.cs
[assembly:ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
)]
Parameters
As it states in the autogenerated comments, the first parameter is to determine wheter there exist or where to find Theme specific resource dictionaries.
The second parameter defines wheter there exist or where to find the generic ResourceDictionary (Generic.xaml).
ResourceDictionaryLocation-Enumeration
The ResourceDictionaryLocation-Enumeration itself is used to specify the location of those dictionaries.
ResourceDictionaryLocation.None
No theme dictionaries exist.
ResourceDictionaryLocation.SourceAssembly
Theme dictionaries exist in the assembly that defines the types being themed.
This expects the ResourceDictionary to be located in a /Themes-Folder. Explanation later.
ResourceDictionaryLocation.ExternalAssembly
Theme dictionaries exist in assemblies external to the one defining the types being themed.
I am not going to explain how this works.
Why /Themes-Folder
Sadly I couldn't find too much about this. If someone has some more info please share.
Have you ever wondered, how styles of a lookless control are being applied?
If one created a lookless Control, he did as follows:
public class MyControl : ControlTemplate
{
static MyControl()
{
// This tells WPF to search for a Style for this type
DefaultStyleKeyProperty.OverrideMetadata(typeof(MyControl)),
new FrameworkPropertyMetadata(typeof(MyControl)));
}
}
In short, Ressources in WPF are located, by searching up the Logical-Tree, then inside Application's Resources and finally inside sth. they call System-Area (this is my translation from German, if you know a better one pls tell).
So depending on ThemeInfo, MyControl propably had its Style inside a ResourceDictionary inside the /Themes-Folder, eg. /Themes/Generic.xaml. And that tells WPF to add the Ressources to the System-Area which finally results in automatically resolving the appropriate style.
Somewhere inside /Themes/Generic.xaml:
<Style TargetType="{x:Type MyControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type MyControl}">
..
</ControlTemplate/>
</Setter.Value>
</Setter>
</Style>
That's why the above ThemeInfoAttribute requires Generic.xaml to be located in a /Themes-Folder. - And somehow, even if in my case the System-Area-Functionality isn't even used for this generic file, this causes those errors. But I wasn't able to find out why.
Sources:
ThemeInfoAttribute Constructor
ResourceDictionaryLocation Enumeration
Windows Presentation Foundation - Das Umfassende Handbuch, Rheinwerk Computing Version 4

Referencing external xaml resources dictionary file

I have a project with several classes (It's a dll project). I added a xaml file (let's call it styles.xaml) in which I declared my most commonly in-use styles.
I have another project (let's call in prog), in a different solution, in which I would like to use the styles written in styles.xaml.
I tried right clicking prog --> add existing item, and I selected the styles.xaml, wrote all the relevant code, and it worked.
The problem is that it copied the styles.xaml file to my prog project's directory (hence I'm now holding two copies of the same resource file). Unlike regular dlls refrencing, it will always look for it in that directory, and not copy it again if it doesn't exist.
In terms of source control, I don't want to save many copies of the same resource file, one for each application, and in the current solution it appears like I must.
I also tried to add resource, and select that file - same issue.
Is there anyway of doing that?
for example you have in the styles.dll:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush x:Key="BlueColor" Color="Blue"/>
<!-- Whatever Styles you need -->
</ResourceDictionary>
in a BlueTheme.xaml
then reference it with:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://siteoforigin:,,,/styles;component/BlueTheme.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>

Categories

Resources