XamlWriter 类

定义

提供一个静态 Save 方法(多次重载),该方法可用于以受限的 XAML 序列化方式,将所提供的运行时对象序列化为 XAML 标记。Provides a single static Save method (multiple overloads) that can be used for limited XAML serialization of provided run-time objects into XAML markup.

public ref class XamlWriter abstract sealed
public static class XamlWriter
type XamlWriter = class
Public Class XamlWriter
继承
XamlWriter

示例

下面的示例XamlWriter使用类Button将序列化为字符串。The following example serializes a Button into a string using the XamlWriter class. 然后, Button使用XamlReader类上的静态Load方法, 将字符串反序列化为。The string is then deserialized back into a Button using the static Load method on the XamlReader class.

// 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);
' Create the Button.
Dim originalButton As New Button()
originalButton.Height = 50
originalButton.Width = 100
originalButton.Background = Brushes.AliceBlue
originalButton.Content = "Click Me"

' Save the Button to a string.
Dim savedButton As String = XamlWriter.Save(originalButton)

' Load the button
Dim stringReader As New StringReader(savedButton)
Dim xmlReader As XmlReader = XmlReader.Create(stringReader)
Dim readerLoadButton As Button = CType(XamlReader.Load(xmlReader), Button)

注解

此方法启用的序列化具有一系列限制。The serialization enabled by this method has a series of limitations. 这是因为启用序列化的是显式运行时, 不能访问原始 XAML (如果有) 中可能的设计时信息。This is because the serialization enabled is explicitly run-time, and does not have access to possible design-time information in the original XAML (if any). 有关详细信息, 请参阅system.windows.markup.xamlwriter.save 的序列化限制For details, see Serialization Limitations of XamlWriter.Save.

Save部分信任环境中运行时, 不允许调用。Calling Save is not permitted when running in partial trust. 这包括从 Xbap。This includes from XBAPs.

方法

Save(Object)

返回用于序列化指定对象及其属性的 XAML 字符串。Returns a XAML string that serializes the specified object and its properties.

Save(Object, Stream)

将 XAML 信息保存到指定流中,以便序列化指定对象及其属性。Saves XAML information into a specified stream to serialize the specified object and its properties.

Save(Object, TextWriter)

将 XAML 信息保存为所提供的 TextWriter 对象的源。Saves XAML information as the source for a provided TextWriter object. 然后,可以使用 TextWriter 的输出对所提供的对象及其属性进行序列化。The output of the TextWriter can then be used to serialize the provided object and its properties.

Save(Object, XamlDesignerSerializationManager)

将 XAML 信息保存到自定义序列化程序中。Saves XAML information into a custom serializer. 然后,可以使用该序列化程序的输出来序列化提供的对象及其属性。The output of the serializer can then be used to serialize the provided object and its properties.

Save(Object, XmlWriter)

将 XAML 信息保存为所提供的 XmlWriter 对象的源。Saves XAML information as the source for a provided XmlWriter object. 然后,可以使用 XmlWriter 的输出对所提供的对象及其属性进行序列化。The output of the XmlWriter can then be used to serialize the provided object and its properties.

适用于

另请参阅