Skip to main content

XAML serialization compatibility

LightningChart .NET supports XAML serialization from version 12.0. This means that .NET class XamlWriter can be used to serialize LightningChart object into XAML markup, while .NET class XamlReader can be used to read XAML input and create a LightningChart object. By using XamlWriter.Save() and XamlReader.Load() methods user can save the state of the Chart (save value for the most of its properties) and import that state next time user opens the application. The example of XAML string of serialized Chart shown in screenshot.

note
  1. Series Data/Points properties are not serialized;
  2. VolumeModel properties are not serialized;
  3. starting from 12.0 version it is perfectly fine to create chart in *.xaml code with non-bindable edition of LightningChart, although if someone needs to bind chart properties, then MVVM edition of LightningChart should be used.

XamlWriter.Save example
Example Xml/XAML file after XamlWriter.Save()

Using global string for serialization

If application is not closed during XAML serialization, then string can be used to store and retrieve state of the Chart. For example, saving XAML with

string SavedXaml = XamlWriter.Save(_chart);

and loading XAML string with

_chart.Dispose();

StringReader stringReader = new StringReader(SavedXaml);
XmlReader xmlReader = XmlReader.Create(stringReader);
_chart = (LightningChart)XamlReader.Load(xmlReader);

Using file for serialization

LightningChart object can be serialize directly to file, and then deserialized from that file. For example, saving XAML with

// Open or create the output file.
using (FileStream xamlFile = new FileStream(@"c:\temp\test.xml", FileMode.Create, FileAccess.ReadWrite))
{
// Save the contents of the Chart to the file stream that was just opened.
XamlWriter.Save(_chart, xamlFile);
}

and loading from XAML file

_chart.Dispose();

using (FileStream xamlFile = new FileStream(@"c:\temp\test.xml", FileMode.Open))
{
_chart = (LightningChart)XamlReader.Load(xamlFile);
}
info

To see feature demonstration as example, check ExampleXamlSerialization from our Demo.

While ExampleXamlSerialization shows how ‘state’ of a chart could be saved and restored, Demo allows to save XAML string of serialized Chart of any example (button ‘Extract Xml’).