Silverlight如何支持多语言

利用资源文件(Resources File)使SilverLight支持多语言。

1. 创建一个SilverLight应用程序, MultiLanguageDemo。

2. 添加一个资源文件,命名为ApplicationStrings.resx。

localized1

3. 添加String类型的资源,这里添加 UsernameString:User Name。设置Access Modeifer为Public。

localized2

4. 为了让程序可以支持中文,再添加一个资源文件:ApplicationStrings.zh-CN.resx。(一定要保证.zh之前的名称与之前的resx一致才行,否则不会读取该文件)这里的zh-CN就是语言区域性名称。关于这个名称可以查阅MSDN。最好是使用zh-Hans这样才能在XP以及WIN7系统下都能正常显示。

localized3

5. 为这个资源文件添加同样的资源,只是这次是用中文写的。UsernameString:姓 名。确保Access Modeifer为No code generation。

localized4

6. 使用资源来显示文本内容。这里可以通过代码实现,也可以做一个ResourcesWrapper的类,用于直接Binding。这里是用Binding的方式。

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace MultiLanguageDemo
{
    public class ResourcesWrapper
    {
        private static MultiLanguageDemo.ApplicationStrings app = new ApplicationStrings();
        public MultiLanguageDemo.ApplicationStrings App
        {
            get { return app; }
        }
    }
}


在MainPage:

<UserControl x:Class="MultiLanguageDemo.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:local="clr-namespace:MultiLanguageDemo"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">
    
    <Grid x:Name="LayoutRoot" Background="White">
        <Grid.Resources>
            <local:ResourcesWrapper x:Key="ResourcesWrapper"/>
        </Grid.Resources>
        <TextBlock Text="{Binding Source={StaticResource ResourcesWrapper},Path=App.UserNameString}"/>
    </Grid>
</UserControl>


 

7. 为了让Silverlight在XAP文件中加入支持的语言资源,我们还需要编辑一下.csproj文件。Unload SilverLight 工程,右键编辑这个文件,在<SupportedCultures>节点中,添加支持的语言,之间用分号隔开。例如:

<SupportedCultures>en-US;zh-CN;zh-Hans</SupportedCultures>

8. 好了,让我们重新加载这个工程,并且编译一下。现在可以看到,在Debug目录下,多出来一个zh-CN的目录,这便是中文支持的语言包了。为了确保XAP中也包含这个文件,可以将XAP用Zip解开,会看到同样包含这个目录。

9. 让我们试一下这个可不可行,直接运行。结果发现,显示出来的还是英文。哦,因为默认UI是显示英文的,修改App.xaml.cs,将UI语言和区域语言一致,当然,这里也可以直接指定使用其他语言。

        private void Application_Startup(object sender, StartupEventArgs e)
        {
            Thread.CurrentThread.CurrentUICulture = CultureInfo.CurrentCulture;
            this.RootVisual = new MainPage();
        }


好了,这样我们就可以让我们的SilverLight程序支持多国语言了。

 

源代码: http://download.csdn.net/detail/eric_k1m/5811615

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值