XAML中的WPF本地化是一种简单,轻松且优雅的方法吗? [英] WPF Localization in XAML, what is the simple, easy and elegant way of doing it?

查看:63
本文介绍了XAML中的WPF本地化是一种简单,轻松且优雅的方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常常见的问题.在WPF应用中进行本地化的最佳方法是什么. 好吧,我在SO中搜索了Binged了很多.但是我找不到任何简单而优雅的指针.

I have a very common question. What is the best way to do localization in a WPF app. Well, I searched in SO and Binged a lot too. But I could not find any pointers which is really simple and elegant.

假设我必须在用户界面中显示以下内容:

Assume that I have to display in UI something like:

英语:组织机构-Beoing

In English: Orgnanization - Beoing

法语:Organizzazione-Beoing

In French: Organizzazione - Beoing

    <StackPanel Orientation="Horizontal">
        <TextBlock Text="Organization -"></TextBlock>
        <TextBlock Text="{Binding Path=OrganizationName}">
        </TextBlock>
    </StackPanel>

基本上,我需要将本地化的文本分配给组织标签TextBlock. 我是否应该将连字符与组织-"分开,并放在一个单独的标签中?

Basically, I need to assign the localized text to Organization Label TextBlock. Should I separate the hyphen from "Organization -" and put it in a separate label?

我该怎么做?

推荐答案

创建项目WpfApplication1

Create a project WpfApplication1

创建文件夹本地化"

在"localization \"中添加资源文件(Strings.resx),并添加字符串"OrganizationText"和值"Organisation"

Add a resource file (Strings.resx) in 'localization\' and add the string 'OrganizationText' and value 'Organisation'

编译时,将生成designer.cs文件.不幸的是,此文件中的所有方法都是内部方法,我们需要公共访问权限才能启用来自wpf的字符串引用.为此,将.resx'自定义工具'替换为'PublicResXFileCodeGenerator'(> = vs2008),然后重新构建. 在MainWindow.xaml中使用此xaml:

When you compile, a designer.cs file is generated. Unfortunatly, all methods in this file are internal and we need public acces to enable string references from wpf. To do that, replace the .resx 'custom tool' with 'PublicResXFileCodeGenerator' (>=vs2008) and build again. Use this xaml in MainWindow.xaml:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:localization="clr-namespace:WpfApplication1.Localization"
        Title="MainWindow" Height="350" Width="525">
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="{x:Static localization:Strings.OrganizationText}"/>
        </StackPanel>
</Window>

要添加'-',必须使用多重绑定(请参阅

To add the ' - ', you must use multibinding (see here)

这篇关于XAML中的WPF本地化是一种简单,轻松且优雅的方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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