2013-02-22 60 views
1

即使我的表单未设置为本地化,如果我有一个标签的文本超过200个字符,Visual Designer会将文本放入.resx而不是直接放入代码中长。如何防止长字符串被写入resx文件

这是问题,因为我使用自定义本地化方法从代码中提取可本地化的字符串,但它不搜索resx文件。

有没有办法来防止这种行为?我可以在我的代码中对字符串进行硬编码,这样提取器就可以得到它,但这很烦人,因为在Designer中文本看起来不对。或者,我可以使用多个标签,但这只是感觉不对。

我见过其他人在其他论坛上提过类似的问题,但没有答案。

回答

0

我没有一个完整的解决方案,但可以扫描与某个类型关联的资源并查找设计者已决定执行此操作的字符串。

  var resources = new ResourceManager(type); 
      using (var set = resources.GetResourceSet(CultureInfo.InvariantCulture, true, false)) 
      { 
       if (set != null) 
       { 
        foreach (DictionaryEntry res in set) 
        { 
         var key = res.Key as string; 
         var val = res.Value as string; 
         if (key == null || val == null) 
          continue; 
         if (!key.EndsWith(".Text")) 
          continue; 
         key = key.Substring(0, key.Length - ".Text".Length); 
         // key is now the name of your control 
         // val is the string the designer stored as its Text in the default resource 
        } 
       } 
      }