从ResourceDictionary中提取和访问格式化文本 [英] Extraction and access of formatted text into/from a ResourceDictionary

查看:131
本文介绍了从ResourceDictionary中提取和访问格式化文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个多语言应用程序。因此,输出的每个文本(在C#-Code和XAML中)也必须保存在(Language-)ResourceDictionary中。这适用于纯文本。例如:



I am writing a multilanguage application. So each text for output (in C#-Code and in XAML too) has to be held in (Language-)ResourceDictionary. This works fine with plain text. E.g:

<!-- The Dictionary for English language -->
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

                    xmlns:s="clr-namespace:System;assembly=mscorlib"

                    >
    <!-- Dialog 17-->
    <s:String x:Key="TextOfTbl7Key">This is the all plain text of TextBlock7 in English</s:String>
    ...
    ...
</ResourceDictionary>







<!-- The application -->
<Window x:Class="MyAppl.Windows.Dialog17"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid>
         .....
         <TextBlock Text="{DynamicResource TextOfTbl7Key}"/>;
         ...
         ...
    </Grid>
</Window>





但是,如果TextBlock带有文本到extract包含格式化文本,即TextBlock具有内联:





But what, if the TextBlock with text to extract contains formatted Text, i.e. the TextBlock has Inlines:

<TextBlock Name="Tbl8">
   Normal stuff, something very <Bold>important</Bold>, and a <Italic>Name</Italic> of something. 
</TextBlock>





我想要的是将其导出为单个...(无论如何 - 但字符串不起作用)。没有解决方案是在许多textparts(使用自己的键)中划分文本,因为文本的组成将取决于语言(而且这个解决方案也会很难看)。一种解决方案是将整个TextBlock作为ResourceDictionary中的资源保持这种方式:





What I want is to export this as a single ... (whatever - but string does not work). No solution is to divide the text in many textparts (with own keys), because the composition of the text will depend on the language (And this solution would be ugly too). One solution could be to hold the entire TextBlock as a resource in the ResourceDictionary this way:

<!-- The Dictionary for English language -->
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

                    xmlns:s="clr-namespace:System;assembly=mscorlib"

                    >
    <!-- Dialog 17-->
    <s:String x:Key="TextOfTbl7Key">This is boring text of TextBlock7 in English</s:String>
    <TextBlock x:Key="Tbl8Key">Normal stuff, something very <Bold>important</Bold>, and a <Italic>Name</Italic> of something </TextBlock>
    ...
    ...
</ResourceDictionary>









但我不知道如何在XAML中作为Elemnet访问Resource-TextBlock。







But I have no idea how to access the Resource-TextBlock as an Elemnet in XAML.

<!-- The application -->
<Window x:Class="MyAppl.Windows.Dialog17"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid>
         .....
         <TextBlock Text="{DynamicResource TextOfTbl7Key}"/>;
         <!-- Here Tbl8 should be instantieted by access to ResourceDictionary --> 
         ...
         ...
    </Grid>
</Window>





感谢您提前的答案。



Thanks for your answers in advance.

推荐答案

可能在.resx文件中存储资源会更好。这是最简单的方法:

通过resx文件添加本地化。它确实适用于简单的字符串。我唯一没检查的是转义的xml标签是否有效。
maybe storing resources in .resx files will work better. Here is a simplest way to do that:
add localization via resx files. It works for sure for simple strings. The only thing I didn''t check is whether escaped xml tags work.


这篇关于从ResourceDictionary中提取和访问格式化文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆