问题描述
此问题类似于
this thead (我甚至把这个标题做得类似于强调事实)。这次不是关于图像,而是关于应用程序资源字典,存储为.xaml文件。
This problem is akin to the one in this thead (I've even made the title similar to emphasize the fact). This time it is not about the images, but about application resource dictionaries, stored as .xaml files.
由于其他欧洲语言不像英语那么紧凑(比较:"已保存的游戏") - "Partidos guardados" - "partysauveguardées" - "Запомненныеигры"),文字可能不适合指定的空间, 在
中,需要将字体样式更改为"Condensed",或者应用缩放 如果需要的话。
Since other European languages are not as compact as English (compare: "Saved Games" - "Partidos guardados" - "Parties sauveguardées" - "Запомненные игры"), the text might not fit in specified space, in which case there is a need either to change font style to "Condensed", or apply scaling if needed.
为了满足这个要求,我曾经有过几份副本。资源字典文件,名为
Dimensions.xaml ,位于不同的目录中,例如 ..... / 烯/ Dimensions.xaml ,  ..... / es-es / Dimensions.xaml 等
To meet this requirement, I used to have several copies of Resource Dictionary file, namedDimensions.xaml, located in separate directories like ...../en/Dimensions.xaml, ...../es-es/Dimensions.xaml, etc
在开始时正确选取文件,但是当我在运行时更改语言时
ApplicationLanguages.PrimaryLanguageOverride, 值未更新。
The file is picked up correctly at start, but when I change language in runtime withApplicationLanguages.PrimaryLanguageOverride, the values are not updated.
我尝试使用以下代码修补此问题:
I tried to patch the problem with the following code:
var toDict = Application.Current.Resources;
var fromDict = new ResourceDictionary()
{ Source = new Uri(ResourceNamePrefix+"Dimensions.xaml"); };
foreach (var key in fromDict.Keys)
toDict[key] = fromDict[key];
但是这不起作用。尝试了另外一个
however this does not work. Tried also
var fromDict = new ResourceDictionary()
Application.LoadObject(fromDict,new Uri(ResourceNamePrefix +" Dimensions.xaml"));
Application.LoadObject(fromDict, new Uri(ResourceNamePrefix+"Dimensions.xaml"));
相同的结果。
喜欢以下情况图片,似乎.Net使用了 预加载(缓存)的ResourceDictionary副本,而不关注语言更改。图像文件的技巧(在引用的线程中讨论)在这种情况下不起作用,因为我看不到将
设置为资源字典的文件或流(而不是Uri)的方法。我不认为XamlReader也可以工作,因为它只接受xaml作为字符串,而所有xaml文件在构建期间都被编译。
Like in case of images, it appears that .Net uses a preloaded (cached) copy of ResourceDictionary without paying attention on language change. The trick for image files (discussed in quoted thread) doesn't work in this case , since I can't see a way to set source as file or stream (rather than Uri) for a resource dictionary. I don't expect XamlReader to work either, given that it only accepts xaml as a string, while all xaml files get compiled during build.
所以,我不得不放弃"幻想"。 NET语言功能"。我没有在我的应用程序的 MergedDictionaries 中使用
Dimensions.xaml ,而是保留了不同的文件名:Dimensions-en.xaml,Dimensions-es.xaml,。 。并在代码中管理语言:
So, I had to drop "fancy .NET language feature". Instead of having Dimensions.xaml in MergedDictionaries for my app, I now keep different file names: Dimensions-en.xaml, Dimensions-es.xaml,.. and manage languages in the code:
internal static void AccommodateLanguageSpecificResources() {
var curLanguage = Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride;
if (curLanguage == null) {
var langs = Windows.Globalization.ApplicationLanguages.Languages;
if (langs != null && langs.Count > 0) curLanguage = langs[0];
}
if (curLanguage == null)
curLanguage = "en";
else
curLanguage = curLanguage.Split(new char[] { '-' })[0];
var resourceUris = LanguageSpecificResourceNames.Select(
name => new Uri(ResourceNamePrefix +
+ name + "-" + curLanguage + ".xaml")).ToList();
var toDict = Application.Current.Resources;
foreach (var uri in resourceUris) {
var fromDict = new ResourceDictionary() { Source = uri };
foreach (var key in fromDict.Keys) toDict[key] = fromDict[key];
}
}
你不喜欢它吗?我也不喜欢它。希望下一个.NET版本能解决这个问题。但是目前还有更好的方法吗?
b
$
Don't you like it? I don't like it either. Hopefully next .NET release will fix this issue. But is there a better way at the moment?
推荐答案
这篇关于[UWP] [C#] ApplicationLanguages.PrimaryLanguageOverride不会影响应用程序资源字典。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!