Compartilhar via


Serialization Limitations of XamlWriter.Save

The API Save can be used to serialize the contents of a Windows Presentation Foundation (WPF) application as a Extensible Application Markup Language (XAML) file. However, there are some notable limitations in exactly what is serialized. These limitations and some general considerations are documented in this topic.

Este tópico contém as seguintes seções.

  • Run-Time, Not Design-Time Representation
  • Serialization is Self-Contained
  • Extension References are Dereferenced
  • Event Handling is Not Preserved
  • Realistic Scenarios for Use of XAMLWriter.Save

Run-Time, Not Design-Time Representation

The basic philosophy of what is serialized by a call to Save is that the result will be a representation of the object being serialized, at run-time. Many design-time properties of the original XAML file may already be optimized or lost by the time that the XAML is loaded as in-memory objects, and are not preserved when you call Save to serialize. The serialized result is an effective representation of the constructed logical tree of the application, but not necessarily of the original XAML that produced it. These issues make it extremely difficult to use the Save serialization as part of an extensive XAML design surface.

Serialization is Self-Contained

The serialized output of Save is self-contained; everything that is serialized is contained inside a XAML single page, with a single root element, and no external references other than URIs. For instance, if your page referenced resources from application resources, these will appear as if they were a component of the page being serialized.

Extension References are Dereferenced

Common references to objects made by various markup extension formats, such as StaticResource or Binding, will be dereferenced by the serialization process. These were already dereferenced at the time that in-memory objects were created by the application runtime, and the Save logic does not revisit the original XAML to restore such references to the serialized output. This potentially freezes any databound or resource obtained value to be the value last used by the run-time representation, with only limited or indirect ability to distinguish such a value from any other value set locally. Images are also serialized as object references to images as they exist in the project, rather than as original source references, losing whatever filename or URI was originally referenced. Even resources declared within the same page are seen serialized into the point where they were referenced, rather than being preserved as a key of a resource collection.

Event Handling is Not Preserved

Quando os manipuladores de eventos que são adicionados por meio de XAML são serializados, eles não serão preservados. XAML sem código - atrás (e também sem o mecanismo relacionados x: Code) não tem como serialização de lógica de tempo de execução do procedimento. Because serialization is self-contained and limited to the logical tree, there is no facility for storing the event handlers. As a result, event handler attributes, both the attribute itself and the string value that names the handler, are removed from the output XAML.

Realistic Scenarios for Use of XAMLWriter.Save

While the limitations listed here are fairly substantial, there are still several appropriate scenarios for using Save for serialization.

  • Saída gráfica ou de vetor: A saída renderizada área pode ser usada para reproduzir o mesmo vetor ou elementos gráficos quando recarregado.

  • Documentos de texto e o fluxo avançados: Texto e todos os contenção de elemento e formatação de elemento dentro dela é preservado na saída. This can be useful for mechanisms that approximate a clipboard functionality.

  • Preservação de dados de objeto de negócios: Se você tiver dados armazenados em elementos personalizados, sistema autônomo XML dados, tão longos quanto seus objetos comerciais seguem básicos XAML regras sistema autônomo fornecer construtores personalizados e conversão de por referência valores de propriedade, esses objetos podem ser preservados por meio de serialização de negócios.