我正在使用单个资源文件(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.fr-CA.resx)将名称空间与默认资源文件(ApplicationStrings.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应用程序中的默认资源完全相同。