- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以标题可能不是很清楚。我有一个 Strings.xaml 文件,其中包含应用程序中使用的几个字符串。
字符串.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>
这个资源文件很好用。我想知道是否有任何内置方式可以使用 Strings.xaml
作为默认资源文件,然后覆盖为不同程序模式自定义的特定字符串?就像让 Strings.xaml
默认并使用 Test_1_Strings.xaml
和 Test_2_Strings.xaml
来覆盖自定义消息的一些字符串。
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 个不同资源文件中的共享条目,我可以只在一个地方完成。
最佳答案
WPF 中的资源查找从下到上遍历,即任何资源使用将首先在其父容器中查找资源,可以是 Grid、StackPanel 等。如果在父容器中找不到资源,则会查找资源在父级的父级容器中,依此类推到 UserControl、Window,直到到达 App 资源。
此外,以后在资源部分下定义的任何资源都会覆盖以前使用相同 key 添加的资源。这适用于在不同资源字典下但不在同一 XAML 文件中定义的资源。如果您尝试用相同的键声明两个项目,它将失败并出现键已存在的异常。
您可以利用上述功能。
假设您要合并 App 资源下的资源,您可以做的是在顶部添加 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_2 之后添加 Test_1,textBlock 文本将为 - This is a message for test 1
。
关于c# - 有默认的字符串资源文件并用自定义的覆盖它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22143142/
我有一个Perl脚本,要求用户输入密码。当用户键入字符时,我该如何仅回声“ *”代替用户键入的字符? 我正在使用Windows XP / Vista。 最佳答案 您可以玩Term :: ReadKey
This question already has answers here: How to urlencode a querystring in Python? (13个回答) 7年前关闭。 我正在
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题吗? 通过 editing this post 添加细节并澄清问题. 关闭 7 年前。 Improve t
我希望能够检测 h1 中的“/”标签,并将其替换为 ,其中.slash将充当分隔符。 var div = $('h1'); div.html(div.html().replace(/\//g, '/
我是一名 Java 初学者,目前正在寻找一种分割字符串的方法 message根据分隔符 (.) 划分为子字符串。理想情况下,我有单个句子,并且我想将每个句子包装在 HTML 标签中,即。 e. 。
MySql:我的产品表设置如下: pg_id |页面名称 1 |披萨馅饼 2 | child 菜单 Php:在循环遍历 MySQL 表中的记录时回显 html。 "; ?> 我
我正在尝试一次读取一个文本文件并将每一行打印到终端窗口。我正在使用 g++ 在 mac 上编译,例如 g++ cpp3.cpp -o cpp3。 文本文件如下所示: 20100000001 20100
这个问题在这里已经有了答案: sed fails with "unknown option to `s'" error [closed] (1 个回答) 关闭 7 年前。 我有个小问题。 我尝试用文
我有一个非常具体的问题。我正在使用 Debian。我有一个 FTP 文件夹,应用程序将在其中上传 pdf 文件,该文件将存储在 ftpfolder/EMAIL_ADDRESS 中,文件名将是 CURR
我尝试使用其 before 和 after 值的 means 填充 NaN 单元格。 type date v1 v2 0 a 2018-09 215
我在上述模拟中使用 SHM 的标准微分方程,a = -w^2*x。我正在使用 Python,并以 odeint 作为求解器。尽管对其进行了多次编辑,我仍然将输出视为直线而不是正弦曲线。代码是: fro
System.out.print("Enter the message to encrypt: "); message = s.next().toString(); // this mess
我有一个主要的 LinearLayout ,在 main.xml 文件中,在 Activity 中设置 ( setContentView )。入主LinearLayout我想添加 1-X 自定义 Li
并用括起来是否安全?
一些用户生成的文本(可能包含脚本/代码)将向公众展示。如果我过滤掉所有 和标记并用 将结果文本括起来和 ,用户提交的脚本有机会被执行吗? 请给我一个如何破解这个保护方案的例子。 最佳答案 Is it
{abc def ghi} 我已经通过在比较它是否是 { 或 } 之前先跨度包装所有字符来完成此操作。但这太慢了,我需要反转该过程,是否可以获取相对于父 div 的 cha
我想创建一个自定义 UITabBarItem 并使用 xib 文件设置其 UI,就像常规 UIView 一样。 我尝试在 UITabBarItem 上创建一个子类,但创建 xib 文件的选项不可用。
我有以下 HTML 结构: Text 1 Text 2 Text 3 Text 4 Text 5 我想找到一个包含“4”
我正在逐字创建着色工具。基本上用户可以从单词中选择几个字母并将它们分开着色,因此一个单词可以有 2 种或更多颜色。 为了跟踪所有单词,它们都有 ID,我想知道我怎么知道选择了哪些字母以及所选单词有哪些
我是 Python 的新手。我确实有一个包含单词列表的文件。它们包含丹麦字母 (ÆØÅ),但 re.compile 不理解这些字符。该函数按每个 ÆØÅ 拆分单词。文本是从 Twitter 和 Fac
C#如何在字符串中找到多余的)或(括号,并替换为@ 示例输入 )(more))) ((((more))) ((((more)) (about)((index)(more))) (about)((ind
我是一名优秀的程序员,十分优秀!