在Silverlight中使用嵌入式资源(4)——其他文化未编译

发布于 2024-09-04 21:40:21 字数 2694 浏览 0 评论 0原文

我在小型 Silverlight 4 应用程序中为 UI 提供本地化字符串时遇到了一些困难。基本上我已经放置了一个文件夹“资源”并在其中放置了两个资源文件:

Statuses.resx
Statuses.ro.resx

我确实有一个枚举状态:

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}}" />

在视图渲染时调用转换器,但无论线程是什么.CurrentThread.CurrentUICulture 设置为始终返回默认区域性值。

经过进一步检查,我拆开了 XAP 结果文件,将结果 DLL 文件带到 Reflector 并检查了嵌入式资源。它只包含默认资源!

回到两个资源文件,我现在正在检查它们的属性:

构建操作:嵌入式资源 复制到输出目录:不复制 自定义工具:ResXFileCodeGenerator 自定义工具命名空间:[空]

两个资源 (.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

[empty]

我已将这两个文件放入控制台应用程序中,它们的行为符合预期,而不是像在此 silverlight 应用程序中。

怎么了?

I am having a bit of a hard time providing localized strings for the UI in a small Silverlight 4 application. Basically I've put a folder "Resources" and placed two resource files in it :

Statuses.resx
Statuses.ro.resx

I do have an enum Statuses :

public enum Statuses
{
    None,
    Working
}

and a convertor :

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();
    }
}

in the view I have a textblock :

    <TextBlock Grid.Column="3" Text="{Binding Status, Converter={StaticResource StatusToMessage}}" />

Upon view rendering the converter is called but no matter what the Thread.CurrentThread.CurrentUICulture is set it always returns the default culture value.

Upon further inspection I took apart the XAP resulted file, taken the resulted DLL file to Reflector and inspected the embedded resources. It only contains the default resource!!

Going back to the two resource files I am now inspecting their properties :

Build action : Embedded Resource
Copy to output directory : Do not copy
Custom tool : ResXFileCodeGenerator
Custom tool namespace : [empty]

Both resource (.resx) files have these settings. The .Designer.cs resulted files are as follows :

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

[empty]

I've taken both files and put them in a console application and they behave as expected in it, not like in this silverlight application.

What is wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤蝉 2024-09-11 21:40:21

事实证明你只需要再做一件有趣的事情。正如 MSDN 文章 所说:

在解决方案资源管理器中,右键单击
项目名称,然后单击“卸载”
项目关闭项目同时
使项目图标保持可见。

在解决方案资源管理器中,右键单击
项目名称,然后单击“编辑”。

项目文件在 Visual 中打开
Studio XML 编辑器。

在项目文件中,添加以下名称
区域中立和具体
其卫星组件的文化
您的应用程序已创建到
<支持的文化>标签。如果你的
应用程序支持多个
文化,使用分号 (;) 来
将他们的名字分开。这份名单
文化不应该包括你的
应用程序的默认区域性。为了
例如,<受支持的文化>标记为
其默认文化的应用程序
是英语(“en”)并且支持
英语 - 美国(“en-US”),
法语 ("fr"), 法语 - 法国
(“fr-FR”)、俄语(“ru”)和俄语
- 俄罗斯(“ru-RU”)文化可能如下所示:

<支持的文化>en-US;fr;fr-FR;ru;ru-RU;

因此,请记住f!@#ingly 手动编辑项目文件并指定要包含在编译中的区域性。

现在它有效了:D

It turns out you just have to do one more tintsy thing. As the MSDN article says :

In Solution Explorer, right-click the
project name, and then click Unload
Project to close the project while
leaving the project icon visible.

In Solution Explorer, right-click the
project name, and then click Edit.

The project file opens in the Visual
Studio XML Editor.

In the project file, add the names of
the region-neutral and specific
cultures whose satellite assemblies
your application has created to the
<SupportedCultures> tag. If your
application supports multiple
cultures, use a semicolon (;) to
separate their names. This list of
cultures should not include your
application's default culture. For
example, a <SupportedCultures> tag for
an application whose default culture
is English ("en") and that supports
the English - United States ("en-US"),
French ("fr"), French - France
("fr-FR"), Russian ("ru"), and Russian
- Russia ("ru-RU") cultures might appear as follows:

<SupportedCultures>en-US;fr;fr-FR;ru;ru-RU;</SupportedCultures>

So, remember to f!@#ingly manual edit the project file and specify which cultures to include in the compilation.

Now it effing works :D

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文