gpt4 book ai didi

c# - 为什么我的应用程序找不到默认的附属程序集 en-US 资源?

转载 作者:太空狗 更新时间:2023-10-29 21:50:31 25 4
gpt4 key购买 nike

我正在使用 WPF 创建一个多语言应用程序。我正在使用静态资源,存储在 .resx/res 文件中的字符串让文本出现在用户的文化中。

这是项目目录结构的一部分:

  • 资源
    • GreetingWindowRes.resx(对于 en-US,这是默认值)
    • GreetingsWindowRes.de-DE.resx

在 .csproj 中,我有:<UICulture>en-US</UICulture>

在 AssemblyInfo.cs 中:[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)]

编译后可以看到资源文件出现:

  • zh-CN/MyApp.resources.dll
  • de-DE/MyApp.resources.dll

所以我绑定(bind)到 GreetingWindow.xaml 中的静态字符串资源:

<windows:MyWindowBase x:Class="CommanderDotNET.MyApp.Windows.GreetingWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:windows="clr-namespace:MyApp.Windows"
xmlns:res="clr-namespace:MyApp.Resources">

<Label Content="{x:Static res:GreetingWindowRes.FirstGreetingText}" />

</windows:MyWindowBase>

结果令我费解:

  1. 如果 UICulture 设置为 de-DE,标签文本将以德语正确显示。
  2. 如果 UICulture 设置为 en-US,System.Windows.Markup.StaticExtension 将抛出 XamlParseException。我看到的 XAML 错误一定是以下原因:

Could not find any resources appropriate for the specified culture or the neutral culture. Make sure "MyApp.Resources.GreetingWindowRes.en-US.resources" was correctly embedded or linked into assembly "MyApp" at compile time, or that all the satellite assemblies required are loadable and fully signed.

我做错了什么?

解决方案:

经过大量搜索,我在这里找到了一个很好的解释: http://www.west-wind.com/weblog/posts/2009/Jun/04/Resx-and-BAML-Resources-in-WPF

显然在使用 UltimateResourceFallbackLocation.Satellite 时+ <UICulture> ,您需要同时拥有主要语言的 ResxResource.resx 和 GreetingWindowRes.en-US.resx 文件(讨厌的重复)。

但是,如果您只想使用 Resx 资源,而不想使用 BAML 本地化,这不是可行的方法,而且情况会好很多。仅使用 Resx 资源的正确方法:

  • AssemblyInfo.cs:[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.MainAssembly)]
  • .csproj:不要添加 <UICulture>
  • 资源命名:ResxResource.resx(主要语言)+ ResxResource.de-DE.resx(附加语言)

最佳答案

我认为当你设置一个UICulture时你必须有相应的resx文件。因此,要解决您的问题,您必须为 en-US 文化创建一个文件(与 Resources.resx 相同)。

我认为(但我可能错了)Resources.resx 用于默认文化,或 PC 拥有的文化。

关于c# - 为什么我的应用程序找不到默认的附属程序集 en-US 资源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19158028/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com