<Application x:Class="CustControls.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/ControlLibrary;component/Styles/ControlResource.xaml"/>
<ResourceDictionary Source="StringLocalization/Dictionary_fr-FR.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Now i want change the ResourceDictionary's source name Source="StringLocalization/Dictionary_fr-FR.xaml" to Source="StringLocalization/Dictionary_en-US.xaml"
What should i do for that.
The MSDN-documentation explains,
"In code, you do not set the Source property. Instead, you must obtain a ResourceDictionary object by either creating one or loading one. One way to load an existing ResourceDictionary to call XamlReader.Load on an existing XAML file stream that has a ResourceDictionary root, then casting the XamlReader.Load return value to ResourceDictionary."
It looks like you can only obtain a ResourceDictionary by either creating one or loading one.
It's also important to understand your purpose with the use of ResourceDictionaries. If you are meant to use them as a 'shared resource' you cannot build the dictionaries using 'Embedded Resource' action. Make sure they are marked as 'Content' and linked properly to their path-locations. Furthermore, its also important to understand how Merged Dictionaries behave in regards to which resource is chosen over the other (taken from the MSDN-documentation):
Resources in a merged dictionary occupy a location in the resource lookup scope that is just after the scope of the main resource dictionary they are merged into. Although a resource key must be unique within any individual dictionary, a key can exist multiple times in a set of merged dictionaries. In this case, the resource that is returned will come from the last dictionary found sequentially in the MergedDictionaries collection. If the MergedDictionaries collection was defined in XAML, then the order of the merged dictionaries in the collection is the order of the elements as provided in the markup. If a key is defined in the primary dictionary and also in a dictionary that was merged, then the resource that is returned will come from the primary dictionary. These scoping rules apply equally for both static resource references and dynamic resource references.
Looking at your code, it seems that you just want to load another ResourceDictionary into your application. If that's all you want then probably adding it to the MergedDictionaries collection may already suffice.
If you want to load one on runtime you can use the following code (or similar to). Just make sure that you don't embed your resources:
try
{
string path = @".\Themes\Dictionary1.xaml";
var xmlTextReader = new XmlTextReader(path);
var resourceDictionary = (ResourceDictionary)XamlReader.Load(xmlTextReader);
Application.Current.Resources.MergedDictionaries.Clear();
Application.Current.Resources.MergedDictionaries.Add(resourceDictionary);
}
catch (Exception exception)
{
Debug.WriteLine(exception.Message);
}
Here's the code in case you need it. Let me know if this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With