简体   繁体   English

具有共享项目和XAML的字符串资源

[英]String Resources with Shared Project and XAML

I'm trying to put all my string resources in one place. 我试图将我所有的字符串资源放在一个地方。 I have created a Shared Project in Visual Studio called MySolution.Shared which contains my string resources file, ie, 我在Visual Studio中创建了一个名为MySolution.Shared的共享项目,其中包含我的字符串资源文件,即

MySolution.Shared/Strings/en-US/Resources.resw

In that file I have defined a single string with key MyKey and value MyString . 在该文件中,我使用键MyKey和值MyString定义了一个字符串。 I have then added a reference to this Shared Project to a Universal Apps project called MySolution.Universal . 然后,我将这个共享项目的引用添加到名为MySolution.Universal的Universal Apps项目中。

Using <TextBlock Name="MyTextBlock" x:Uid="MyKey"/> in XAML neither shows anything nor throws an exception. 在XAML中使用<TextBlock Name="MyTextBlock" x:Uid="MyKey"/>既不会显示任何内容,也不会引发异常。 Using MyTextBlock.Text = ResourceLoader.GetForCurrentView().GetString("MyKey") in code behind does work. 在后面的代码中使用MyTextBlock.Text = ResourceLoader.GetForCurrentView().GetString("MyKey")可以正常工作。

So, how do I correctly reference shared string resources in a Shared Project directly from XAML? 因此,如何直接从XAML正确引用共享项目中的共享字符串资源?

The reason is that WPF doesn't know which property of "MyTextBlock" should the value of "MyKey" be assigned to. 原因是WPF不知道应将“ MyKey”的值分配给“ MyTextBlock”的哪个属性。

So you need to change the key of "MyKey" to "MyKey.Text". 因此,您需要将“ MyKey”的键更改为“ MyKey.Text”。 this means that any element with the x:Uid="MyKey" should have its text property set to "MyString". 这意味着任何带有x:Uid =“ MyKey”的元素都应将其text属性设置为“ MyString”。

It is mostly useful for Localizable UI's and when you want to set RTL or LTR flow direction of elements, or their fonts, etc... 它对于可本地化的UI以及要设置元素或其字体等的RTL或LTR流向时非常有用。

example: (in resources) 示例:(在资源中)

<data name="Inputs.FontFamily" xml:space="preserve">
<value>Segoe UI</value> </data>

and in xaml: 并在xaml中:

<TextBox x:Uid="Inputs" TextWrapping="Wrap" VerticalAlignment="Stretch" Margin="0"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM