XamlWriter 类

定义

提供一个静态 Save 方法(多次重载),该方法可用于以受限的 XAML 序列化方式,将所提供的运行时对象序列化为 XAML 标记。

C#
public static class XamlWriter
继承
XamlWriter

示例

以下示例使用 XamlWriter 类将 序列化Button为字符串。 然后,在 类上使用XamlReader静态Load方法将字符串反序列化回 Button

C#
// Create the Button.
Button originalButton = new Button();
originalButton.Height = 50;
originalButton.Width = 100;
originalButton.Background = Brushes.AliceBlue;
originalButton.Content = "Click Me";

// Save the Button to a string.
string savedButton = XamlWriter.Save(originalButton);

// Load the button
StringReader stringReader = new StringReader(savedButton);
XmlReader xmlReader = XmlReader.Create(stringReader);
Button readerLoadButton = (Button)XamlReader.Load(xmlReader);

注解

此方法启用的序列化有一系列限制。 这是因为启用的序列化是显式运行时,如果有任何) ,则无法访问原始 XAML (中可能的设计时信息。 有关详细信息,请参阅 XamlWriter.Save 的序列化限制

在部分信任中运行时,不允许调用 Save 。 这包括来自 XBAPs 的 。

方法

Save(Object)

返回用于序列化指定对象及其属性的 XAML 字符串。

Save(Object, Stream)

将 XAML 信息保存到指定流中,以便序列化指定对象及其属性。

Save(Object, TextWriter)

将 XAML 信息保存为所提供的 TextWriter 对象的源。 然后,可以使用 TextWriter 的输出对所提供的对象及其属性进行序列化。

Save(Object, XamlDesignerSerializationManager)

将 XAML 信息保存到自定义序列化程序中。 然后,可以使用该序列化程序的输出来序列化提供的对象及其属性。

Save(Object, XmlWriter)

将 XAML 信息保存为所提供的 XmlWriter 对象的源。 然后,可以使用 XmlWriter 的输出对所提供的对象及其属性进行序列化。

适用于

产品 版本
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

另请参阅