13

我有一个具有以下用户控件的 XBAP 应用程序:

  <UserControl x:Class="XXX.UsersGrid"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Height="Auto" Width="Auto">

        <UserControl.Resources>
            <DataTemplate x:Key="UpArrowUsers">
                <DockPanel>
                    <TextBlock Text="xxUser" x:Name="upArrowUsersHeader" HorizontalAlignment="Center"></TextBlock>
                    <Path x:Name="arrow" StrokeThickness = "1" Fill= "gray" Data= "M 5,10 L 15,10 L 10,5 L 5,10"/>
                </DockPanel>
            </DataTemplate>
    </UserControl>
    ...

现在我想从作为资源嵌入到应用程序中的 resx 文件中获取字符串“xxUser”我该如何实现?

4

6 回答 6

47

我能够在一个程序中做到这一点:

<TextBlock VerticalAlignment="Center" Margin="3"
           Text="{x:Static prop:Resources.OpenButton}"
           Visibility="{Binding Source={x:Static prop:Settings.Default}, Path=ShowButtonText, 
           Converter={StaticResource BoolToVis}}"></TextBlock>

我还必须在我的 xaml 中包含 .Properties 命名空间,如下所示:

xmlns:prop="clr-namespace:MyProjectNamespace.Properties"

这使我不仅可以使用为我的项目定义的字符串资源进行全球化,而且我还能够(两种方式)绑定到我的应用程序的设置。这让我很容易记住窗口的位置、大小等。如您所见,使用设置。用于设置和资源。对于资源。

正如 Steven 所提到的,我认为“官方”方式或“最佳”方式是将 x:Uid 粘贴在您想要全球化的所有内容上,但我没有这样做,而且它没有任何问题。我认为如果您使用自动化工具或像在大型项目中那样分解翻译任务,则最需要 x:Uid 东西。我只是在 VS 中手动完成了我自己的所有工作,所以也许没问题。

于 2010-01-31T02:45:06.190 回答
9

我在上面“我能够做到……”中忘记提到的另外两点:

  1. 您不必自己包装 Properties.Resources 对象。您可以直接访问它,就像我在上面的示例中所做的那样。我认为包装对象是一种获得与我在第二点中讨论的相同结果的方法。
  2. 默认情况下,资源文件是使用“ResXFileCodeGenerator”构建的。这使得它们在生成代码文件时成为内部文件,因此 xaml 无法访问它。您必须将其更改为“ PublicResXFileCodeGenerator ”,它会生成公共类。您可以通过单击解决方案资源管理器中的资源文件并编辑“自定义工具”属性来更改此设置。

(对不起,我不能编辑我的上面的帖子,因为我当时是临时会员。)

于 2010-01-31T04:36:44.347 回答
7

正如所说,我找到了另一个教程。的应该从更改access modifier为。我失败了很多次,在更改为之后,它确实有效。Resources.resxInternalPublicaccess modifierPublic

于 2013-04-02T05:47:07.533 回答
6

创建一个静态类,使资源可用作属性:

public static class Resources
{
   public string Resource
   {
      return Properties.Resources.ResourceManager.GetString("Resource");
   }
}

然后你可以将你的 TextBox 绑定到这个:

<TextBlock Text="{Binding Source={x:Static local:Resources}, Path=Resource}" x:Name="upArrowUsersHeader" HorizontalAlignment="Center"
   xmlns:local="clr-namespace:MY_NAMESPACE;assembly=MY_ASSEMBLY">
于 2009-03-20T13:35:17.897 回答
4

这些答案都不是你想要的。我将从阅读 WPF 中的本地化开始。您会发现,如果您使用 WPF 进行本地化,您需要在应用程序的每个节点上定义 x:Uid。

http://msdn.microsoft.com/en-us/library/ms788718.aspx

于 2009-03-20T17:24:52.120 回答
1

我不知道这是否可以直接在 XAML 中完成,但如果您围绕 ResourceManager 编写自己的包装类并改用它。请注意,该类继承自 TextBlock:

public class ResourceContentTextBlock : TextBlock
{
    public string ResourceName 
    {
        set
        {
            this.Text = Properties.Resources.ResourceManager.GetString(value);
        }
    }
}

然后,您可以在 XAML 中使用 TextBlock 的任何地方使用 ResourceContentTextBlock:

<Window x:Class="WpfApplication3.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:client="clr-namespace:WpfApplication3" 
    >
        <client:ResourceContentTextBlock ResourceName="String1" />
</Window>
于 2009-03-20T12:37:41.000 回答