gpt4 book ai didi

c# - 访问嵌套在 .cs 文件下的 .resx 文件的资源包装器时出现 MissingManifestResourceException

转载 作者:行者123 更新时间:2023-11-30 12:09:19 29 4
gpt4 key购买 nike

当使用 .csproj 中的 DependentUpon 元素将 .resx 文件嵌套在 .cs 文件下时文件,然后使用包装器访问资源时抛出 MissingManifestResourceException

消息内容如下:

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

示例代码:

MessageBox.Show(Resource.Key);

为什么会出现这个错误?

最佳答案

MSBuild 目标 PrepareResourceNames(也调用 ResGenPrepareResources 依赖)调用名为 CreateManifestResourceName< 的任务.
[参见 Microsoft.Common.targets]

此任务创建 .resources 文件的名称。对于 C#,创建名称的实际方法是 Microsoft.Build.Tasks.CreateCSharpManifestResourceName 中的 CreateManifestNameImpl。此方法检查 .resx 文件是否有一个 .cs 文件作为父文件。在这种情况下,它会在 .cs 文件中搜索它可以找到的第一个完全限定的类名,并将其作为 .resources 文件名。
[参见 Microsoft.Build.Tasks.v4.0.dll]

.resx 包装器不期望此文件名更改,因此抛出异常。

我猜这个行为是为 WinForms 本地化实现的,它将其资源嵌套在相应的表单或控件下。

我决定不嵌套 .resx 文件。一个可能的解决方案是实现 CreateCustomManifestResourceName 任务,MSBuild 为其提供了一个 Hook 。

关于c# - 访问嵌套在 .cs 文件下的 .resx 文件的资源包装器时出现 MissingManifestResourceException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27232539/

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