XamlReader を使用して、定義済みの xaml ファイルからアプリのメニューを読み込むときに、非常に奇妙な問題が見つかりました。の属性を定義する必要がxml:space="preserve"
あり、xaml は次のようになります。
<m:MenuManager>
...
<m:Resource>
<m:Resource.ResourceTitle>
<sys:String xml:space="preserve">Click the Button
(InvokeCommandAction)
View</sys:String>
</m:Resource.ResourceTitle>
</m:Resource>
...
</m:MenuManager>
xamlコンテンツを文字列に読み込み、これを使用XamlReader.Load
して MenuManager オブジェクトに変換します。メソッドが最初にXamlReader.Load
呼び出されると、 tag 内の単語<sys:String xml:space="preserve">
のみが返され、2 回目にのみ期待される結果が返されます。
var uri = new Uri("/Sample;component/Assets/Menu.xaml", UriKind.Relative);
var info = Application.GetResourceStream(uri);
string xaml = null;
using (StreamReader reader = new StreamReader(info.Stream))
{
xaml = reader.ReadToEnd();
}
//when the first time load, only a string value of
//"Click the Button
(InvokeCommandAction)
View" is returned
var temp1 = XamlReader.Load(xaml);
//when the second time load, all menu content loaded successfully and
//converted to the object of MenuManager
readXaml = XamlReader.Load(xaml) as MenuManager;
属性を削除するxml:space="preserve"
か変更するとxml:space="default"
、正常に機能し、メソッドを 1 回呼び出すだけで MenuManager のオブジェクトを取得できますXamlReader.Load
。誰でもこれを説明できますか?ありがとう!