And*_*nea 3 silverlight resources localization embedded-resource silverlight-4.0
我在使用小型Silverlight 4应用程序时为UI提供本地化字符串时遇到了一些困难.基本上我放了一个文件夹"Resources"并在其中放置了两个资源文件:
Statuses.resx
Statuses.ro.resx
Run Code Online (Sandbox Code Playgroud)
我有一个enum状态:
public enum Statuses
{
None,
Working
}
Run Code Online (Sandbox Code Playgroud)
和转换器:
public class StatusToMessage : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (!Enum.IsDefined(typeof(Status), value))
{
throw new ArgumentOutOfRangeException("value");
}
var x = Statuses.None;
return Statuses.ResourceManager.GetString(((Status)value).ToString(), Thread.CurrentThread.CurrentUICulture);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
Run Code Online (Sandbox Code Playgroud)
在视图中我有一个文本块:
<TextBlock Grid.Column="3" Text="{Binding Status, Converter={StaticResource StatusToMessage}}" />
Run Code Online (Sandbox Code Playgroud)
在视图呈现时调用转换器,但无论Thread.CurrentThread.CurrentUICulture设置什么,它始终返回默认的文化值.
在进一步检查后,我拆开了XAP结果文件,将生成的DLL文件带到Reflector并检查了嵌入的资源.它只包含默认资源!!
回到我正在检查其属性的两个资源文件:
构建操作:嵌入式资源复制到输出目录:不复制自定义工具:ResXFileCodeGenerator自定义工具命名空间:[empty]
两个资源(.resx)文件都具有这些设置..Designer.cs结果文件如下:
Statuses.Designer.cs:
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.1
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace SilverlightApplication5.Resources {
using System;
/// <summary>
/// A strongly-typed resource class, for looking up localized strings, etc.
/// </summary>
// This class was auto-generated by the StronglyTypedResourceBuilder
// class via a tool like ResGen or Visual Studio.
// To add or remove a member, edit your .ResX file then rerun ResGen
// with the /str option, or rebuild your VS project.
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
internal class Statuses {
// ... yadda-yadda
Run Code Online (Sandbox Code Playgroud)
Statuses.ro.Designer.cs
[空]
我已经将两个文件都放在一个控制台应用程序中,它们的行为与预期一致,而不像在这个Silverlight应用程序中.
怎么了?
事实证明你只需要做一件有趣的事情.正如MSDN文章所说:
在"解决方案资源管理器"中,右键单击项目名称,然后单击"卸载项目"以关闭项目,同时保持项目图标可见.
在"解决方案资源管理器"中,右键单击项目名称,然后单击"编辑".
项目文件在Visual Studio XML编辑器中打开.
在项目文件中,将应用程序已创建其附属程序集的区域中性和特定区域的名称添加到< SupportedCultures >标记.如果您的应用程序支持多种文化,请使用分号(;)分隔其名称.此文化列表不应包含您的应用程序的默认文化.例如,默认文化为英语("en")且支持英语 - 美国("en-US"),法语("fr"),法语 - 法语("fr"的应用程序的< SupportedCultures >标签-FR"),俄语("ru")和俄罗斯 - 俄罗斯("ru-RU")文化可能如下所示:
<SupportedCultures>en-US;fr;fr-FR;ru;ru-RU;</SupportedCultures>
所以,记住f!@#ingly手动编辑项目文件并指定要包含在编译中的文化.
现在它起作用了:D
| 归档时间: |
|
| 查看次数: |
2501 次 |
| 最近记录: |