我有一個XAML資源字典如下自定義在XAML ResourceDictionary中屬性
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:loc="http://temp/uri"
mc:Ignorable="loc">
<sys:String x:Key="ResouceKey" loc:Note="This is a note for the resource.">ResourceText</sys:String>
</ResourceDictionary>
我通過所有的.xaml文件中循環,並試圖轉換爲POCO類,並得到組委會:注意屬性以及使用XamlReader。
using (var sr = new StreamReader(...))
{
var xamlreader = new XamlReader();
var resourceDictionary = xamlreader.LoadAsync(sr.BaseStream) as ResourceDictionary;
if (resourceDictionary == null)
{
continue;
}
foreach (var key in resourceDictionary.Keys)
{
// In this loop i would like to get the loc:Note
var translation = new Translation
{
LocaleKey = locale,
Note = string.Empty, // How to get the note from the xaml
Text = resourceDictionary[key] as string
});
}
}
這是可能的還是我不得不訴諸使用自定義XML序列化邏輯?
嗨,我們正在使用這些作爲wpf應用程序的資源文件,因此使用自定義類型可能會造成一些不便。我只是試圖將這些提取到一個自定義的本地化引擎,然後將生成xaml資源並映射引擎的實際本地化。不過,我可能能夠按照您的建議調整源材料xaml文件。 –
感謝您的額外信息。它更有意義。在這種情況下,如果你的文件結構足夠簡單,將它解析爲XML可能是最簡單的路線。你需要代碼嗎? –
如果你準備好了,那很棒,謝謝。但我想我可以管理。 –