2

我正在使用单个资源文件 (ApplicationStrings.fr-CA.resx) 动态下载具有嵌入式资源程序集的 XAP 文件。我正在使用 WebClient 下拉 XAP 文件并使用以下代码加载程序集,基于 Jeff Prosise 在这篇文章中所做的工作:http: //www.wintellect.com/CS/blogs/jprosise/archive/2010 /06/21/dynamic-localization-in-silverlight.aspx

请注意,我还使用程序集和 ApplicationManifest.xaml 从 fr-CA 文件夹手动创建 XAP 文件,正如 Guy Smith-Ferrier 在他的演示文稿中列出的步骤所述http://www.guysmithferrier.com/post/2010/ 10/Building-Localized-XAP-Resource-Files-For-Silverlight-4.aspx

  // Get the application manifest from the downloaded XAP
  StreamResourceInfo sri = new StreamResourceInfo(e.Result, null);
  XmlReader reader = XmlReader.Create(Application.GetResourceStream(sri, new   Uri("AppManifest.xaml", UriKind.Relative)).Stream);

  AssemblyPartCollection parts = new AssemblyPartCollection();

  // Enumerate the assemblies in the downloaded XAP
  if (reader.Read())
  {
      reader.ReadStartElement();

      if (reader.ReadToNextSibling("Deployment.Parts"))
      {
          while (reader.ReadToFollowing("AssemblyPart"))
          {
              parts.Add(new AssemblyPart() { Source = reader.GetAttribute("Source") });
          }
      }
  }

  // Load the satellite assemblies
  foreach (AssemblyPart part in parts)
  {
      if (part.Source.ToLower().Contains("resources"))
      {
          Stream assembly = Application.GetResourceStream(sri, new Uri(part.Source,  UriKind.Relative)).Stream;
          part.Load(assembly);
      }
  }

  // Change the culture
  Thread.CurrentThread.CurrentCulture = culture;
  Thread.CurrentThread.CurrentUICulture = culture;

该程序集似乎加载正常,并且我已将名称空间与默认资源文件 (ApplicationStrings.resx) 与下载的资源文件 (ApplicationStrings.fr-CA.resx) 匹配。如代码所示,文化是为当前线程设置的。

但是,对 ApplicationStrings.ResourceManager.GetString(...) 的调用不会返回设置区域性的资源。例如,以下内容应返回新区域性 (fr-CA) 的字符串,但始终返回默认区域性 (en-US)。

 /// <summary>
    ///   Looks up a localized string similar to User Name:.
    /// </summary>
    public static string Label_UserName {
        get {
            return ResourceManager.GetString("Label_UserName", resourceCulture);
        }
    }

有什么建议么?谢谢。

* *更新

我想通了......我忘记在我的卫星装配项目文件中重置我支持的本地人:

<SupportedCultures>fr-CA</SupportedCultures>

我还使我的文件夹结构与我的主 Silverlight 应用程序中的默认资源完全一样。

4

0 回答 0