Create a default resource file and override it with custom

Thus, the name may not be entirely clear. I have a Strings.xaml file that contains several lines that 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 fine. I am wondering if there is any built-in way to use Strings.xamldefault resources as a file, and then override certain lines that are common to different program modes? As Strings.xamlby default, use Test_1_Strings.xamland Test_2_Strings.xamlto override some lines for custom posts.

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>

, , , , , . , 8 , .

+4
1

WPF , , Grid, StackPanel .. , .. UserControl, , .

, , , , . , , XAML. , .


.

, , Strings.xaml , Test_1_Strings.xaml Test_2_Strings.xaml. , , , , .

<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>

, TextBlock StaticResource TestMessage.

<TextBlock Text="{StaticResource TestMessage}"/>

This is a message for test 2.

Test_1 Test_2, textBlock Text - This is a message for test 1.

+1

Source: https://habr.com/ru/post/1529763/


All Articles