本地化格式化XAML使用UID自動是不可能的,但動態XAML可以通過從.resw文件C#代碼被加載。
例如,在resw我有鑰匙MY_FORMATTED_XAML
該值:
<Span Name="SPAN_NAME" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<Run>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque sodales augue sit amet eros cursus, vitae tincidunt velit dictum. Vivamus in dolor sapien. Nunc ac dictum nulla.</Run>
<LineBreak/>
<Run Text="------------"/>
<LineBreak/>
<Run>
Ut mollis massa ut tortor commodo cursus. Etiam at nisl eu lectus ornare iaculis. Sed consequat dignissim sapien, a tincidunt dolor rutrum at. Suspendisse accumsan eget ante ac aliquet. Nulla non dictum nisi. Nam ultrices libero ut orci pretium, finibus mollis mauris varius. Duis suscipit sapien cursus sagittis blandit. Donec hendrerit varius facilisis.
</Run>
<LineBreak/>
<LineBreak/>
<Run>
<Bold>This is a sample for stackoverflow</Bold>
</Run>
<LineBreak/>
<Run>
Nunc mollis porttitor arcu ac mattis. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Sed lacinia mattis enim, a tincidunt nisi. Quisque accumsan, sapien quis fringilla pharetra, purus metus maximus velit, ac aliquam enim justo et ante. Aliquam malesuada at velit vel consequat.
</Run> 
<Italic>License:</Italic>
<Hyperlink NavigateUri="http://unlicense.org">Unlicense 2016.</Hyperlink>
<LineBreak/>
</Span>
在C#中我有一個輔助類加載資源字符串:
public static class ResourceRetriever
{
private static ResourceMap _resourceMap = null;
public static ResourceMap ResourceMap
{
get
{
if (_resourceMap == null)
{
_resourceMap= Windows.ApplicationModel.Resources.Core.ResourceManager.Current.MainResourceMap;
}
return _resourceMap;
}
}
public static string GetString(string key)
{
return ResourceMap?.GetValue("Resources/" + key, new ResourceContext())?.ValueAsString;
}
public static string GetString(string key, ResourceContext context)
{
return ResourceMap?.GetValue("Resources/"+key, context)?.ValueAsString;
}
}
我加載與動態XAML此代碼:
String dynamicXaml = ResourceRetriever.GetString("MY_FORMATTED_XAML");
Span span = XamlReader.Load(dynamicXaml) as Span;
this.MyTextBlock.Inlines.Add(span);
[全球化和本地化](https://msdn.microsoft.com/en-us/windows/ UWP /全球化/全球化門戶)。 – IInspectable
@IInspectable,我已經閱讀文檔,我本地化的所有字符串,但我無法本地化格式的文本。 –
我不明白,爲什麼格式化會有所作爲。這個過程是不是一樣的(除非你不翻譯格式化指令)? – IInspectable