繁体   English   中英

本地化WPF应用程序不起作用?

[英]Localising a WPF application doesn't work?

我必须在这里遗漏一些东西。

我在VS2015中创建了一个全新的WPF应用程序。 我创建了一个资源'String1'并将值设置为'fksdlfdskfs'。

我更新默认的MainWindow.xaml.cs,以便构造函数具有:

    public MainWindow()
    {
        InitializeComponent();
        this.Title = Properties.Resources.String1;
    }

并运行该应用程序,它工作正常,我的窗口标题是fksdlfdskfs。

在AssemblyInfo.cs文件中,我看到以下注释:

//In order to begin building localizable applications, set 
//<UICulture>CultureYouAreCodingWith</UICulture> in your .csproj file
//inside a <PropertyGroup>.  For example, if you are using US english
//in your source files, set the <UICulture> to en-US.  Then uncomment
//the NeutralResourceLanguage attribute below.  Update the "en-US" in
//the line below to match the UICulture setting in the project file.

//[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)]

所以我将以下内容添加到我的WpfApplication5.csproj文件中并在VS中重新加载项目:

<UICulture>en-US</UICulture>

然后在AssemblyInfo.cs中取消注释以下行:

[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)]

如果我现在去运行应用程序,应用程序不再运行,我在读取资源的行上得到以下异常:

System.Resources.MissingManifestResourceException:找不到适合指定文化或中性文化的任何资源。 确保在编译时将“WpfApplication5.Properties.Resources.en-US.resources”正确嵌入或链接到程序集“WpfApplication5”中,或者所有所需的附属程序集都是可加载和完全签名的。

如果我改变UltimateResourceFallbackLocation.SatelliteUltimateResourceFallbackLocation.MainAssembly AssemblyInfo.cs文件中,我得到下面的异常,而不是:

System.IO.IOException:找不到资源'mainwindow.xaml'

我做错了什么或我错过了什么?

你没有被迫使用代码来进行本地化,你可以简单地使用x:Static标记扩展来绑定到静态字段:

<Window
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:properties="clr-namespace:SandBox.Properties"
    Title="{x:Static properties:Resources.TitleSandbox}">

</Window>

只需确保您的资源文件访问修饰符设置为公共

屏幕资源文件

您获得的错误消息通常意味着您没有Resource.en-US.resx文件,因为[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)]告诉您的应用使用en en-US资源file作为默认源。 如果要快速排除错误,请添加名为Resources.en-US.resx的文件

我个人做的本地化WPF应用程序是:

  • 我按原样保留AssemblyInfo.cs ,这意味着Resource.resx (没有语言ID)文件将是默认文件(通常是en-US)
  • 我在默认旁边创建了额外的Resource.{id}.resx文件,如下所示: 样品

    它通常与Resource.resx相同,但在匹配语言中进行翻译

  • 我在启动时(通常在App.xaml.cs )使用用户可设置的语言ID强制文化,以便用户可以更改应用程序语言:
// language is typically "en", "fr" and so on
var culture = new CultureInfo(language);
CultureInfo.DefaultThreadCurrentCulture = culture;
CultureInfo.DefaultThreadCurrentUICulture = culture;
// You'll need to restart the app if you do this after application init

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM