我在使用小型Silverlight 4应用程序时为UI提供本地化字符串时遇到了一些困难 . 基本上我放了一个文件夹“Resources”并在其中放置了两个资源文件:
Statuses.resx
Statuses.ro.resx
我有一个enum状态:
public enum Statuses
{
None,
Working
}
和转换器:
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();
}
}
在视图中我有一个文本块:
<TextBlock Grid.Column="3" Text="{Binding Status, Converter={StaticResource StatusToMessage}}" />
在视图呈现时调用转换器,但无论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
Statuses.ro.Designer.cs
[空]
我已经把这两个文件放在一个控制台应用程序中,它们的行为与预期的一样,不像在这个Silverlight应用程序中 .
怎么了?
1 回答
事实证明你只需要做一件有趣的事情 . 正如MSDN article所说:
所以,记住f!@#ingly手动编辑项目文件并指定要包含在编译中的文化 .
现在它起作用了:D