【问题标题】:WPF get string from resources throws an exceptionWPF 从资源中获取字符串引发异常
【发布时间】:2021-01-03 08:24:43
【问题描述】:

我一直在尝试从资源文件中获取字符串:

var resourceManager = new ResourceManager(typeof(Properties.Settings));
var recent1 = resourceManager.GetString("recent1");

但我遇到了一个例外:

System.Resources.MissingManifestResourceException: '找不到任何 适合特定文化或中立的资源 文化。确保“ProCodeX.Properties.Settings.resources”是 在编译时正确嵌入或链接到程序集“ProCodeX”中, 或者所需的所有附属程序集都是可加载且完全可加载的 签名。

我该如何解决?

【问题讨论】:

  • 你可以使用var str = Properties.Resources.recent1;来获取字符串值。向资源中添加字符串时,模板使用属性的internal static 签名。

标签: c# wpf resources


【解决方案1】:

为了从 .resx 文件中检索字符串,不需要创建 ResourceManager 类实例。向资源添加字符串时,模板使用internal static 签名作为属性。因此可以直接获取资源:

var recent = Properties.Resources.recent1; 

下面是一个示例,当需要获取特定文化的资源时,如何通过创建 ResourceManager 的实例来获取字符串:

ResourceManager rm = new ResourceManager("WpfApp11.Properties.Resources", typeof(Properties.Resources).Assembly);
CultureInfo culture = CultureInfo.CreateSpecificCulture("en-US");
Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;
var recent1 = rm.GetString("recent1");

有关更多信息,请参阅ResourceManager.GetString Method

【讨论】:

    猜你喜欢
    • 2021-07-01
    • 1970-01-01
    • 2015-09-17
    • 1970-01-01
    • 1970-01-01
    • 2011-05-24
    • 2014-05-11
    • 2010-10-14
    • 2018-03-23
    相关资源
    最近更新 更多