.NET 4.0中的XAML序列化堆栈发生了很多变化。其中一个变化是,当你序列化一个UserControl时,你不仅可以获得控件本身,而且还可以获得它的所有子控件。
var sb = new StringBuilder();
var writer = XmlWriter.Create(sb, new XmlWriterSettings
{
Indent = true,
ConformanceLevel = ConformanceLevel.Fragment,
OmitXmlDeclaration = true
});
var mgr = new XamlDesignerSerializationManager(writer);
mgr.XamlWriterMode = XamlWriterMode.Expression;
System.Windows.Markup.XamlWriter.Save(this, mgr);
return sb.ToString();
而不是获得,例如,
<MyUserControl
xmlns="clr-namespace:MyNamespace"
SomeProperty="Add ten thousand child controls" />
你现在得到了
<MyUserControl
xmlns="clr-namespace:MyNamespace"
SomeProperty="Add ten thousand child controls">
<StackPanel xmlns="http://microsoft.com/something/xaml/dude">
<TextBlock Text="Child Control ONE!"/>
<TextBlock Text="Child Control TWO!"/>
<TextBlock Text="Child Control THREE!"/>
<!--WTMFH?-->
<TextBlock Text="Child Control TEN FRIGGEN THOUSAND!"/>
</StackPanel>
</MyUserControl/>
如何将此行为还原为原始方法?