I found a very strange issue when to use XamlReader to load the menu of my app from a predefined xaml file.
I need to define the attribute of xml:space="preserve"
and the xaml is like this:
<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>
Load the xaml content to the string and use XamlReader.Load
to convert it to the MenuManager object. When at the first time the XamlReader.Load
method is called, it will only return the words within the tag <sys:String xml:space="preserve">
, and the expected result only returned at the second time.
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;
When I remove the attribute xml:space="preserve"
or change it to xml:space="default"
it will work fine and I can get the object of MenuManager by calling the method XamlReader.Load
only once.But I really need keep the whitespace on my page, and the code here looks so strange. Can anybody please explain this?
Thanks!