Serialization (saving and loading diagrams)

Back to table of contents

Step by Step:

Serialization (saving  and  loading diagrams)

The template of an application in which the end user can load and save his diagrams.

Step 1

Create new project in Microsoft Visual Studio environment. Select item New\Project from the main menu.



Select Silverlight Application, set name of the project – “Serialization”, set directory to save the project to.



While creation of the project uncheck option “Host the Silverlight application in a new Web site”.




After that Visual Studio will create a new solution containing one project.

Step 2

Add and set up SharpShooter Diagrams component. It’s necessary to add SharpShooter Diagrams component to the Silverlight application to add the diagrams designer. In order to do that, you should add references to the following assemblies:

Aphalina.Core.dll

Aphalina.Diagrams.Components.dll

Aphalina.Diagrams.DesignTime.dll

Aphalina.Diagrams.DesignTime.UI.dll

Aphalina.Diagrams.dll

Aphalina.Diagrams.FlowCharts.DesignTime.dll

Aphalina.Diagrams.FlowCharts.dll

Aphalina.Diagrams.UI.Controls.dll

Aphalina.Diagrams.UI.Models.dll

Aphalina.Diagrams.UI.Ribbon.dll

Aphalina.Drawing.Connections.DesignTime.dll

Aphalina.Drawing.Connections.dll

Aphalina.Drawing.Core.dll

Aphalina.Drawing.dll

Aphalina.Drawing.UI.Controls.dll

Aphalina.Drawing.UI.Models.dll

Aphalina.Drawing.UI.Ribbon.dll

Aphalina.Framework.dll

In order to do it, right-click References of the OnSelectionChanged in the "Solution Explorer" and select “Add Reference” item in the popup menu.





You should also add references to the following assemblies:

System.Windows.Controls.dll

System.Windows.Controls.Input.dll

System.Windows.Controls.Layout.Toolkit.dll

System.Windows.Controls.Toolkit.dll



Your References should look like this:



Step 3

In order to view and change already created diagrams we need to add DiagramComponent control to the MainPage.xaml. Open it in the designer. Right-click MainPage.xaml and select View Designer in the popup menu.



Add xml namespace for the diagrams. Then add DiagramComponent and the panel with “Save diagram” and “Load diagram” buttons. After these steps Main.Page.xaml code should look like this:

<UserControl x:Class="Serialization.MainPage"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

             xmlns:diagrams="http://schemas.aphalina.com/diagrams"

    mc:Ignorable="d"

    d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot" Background="White" Loaded="LayoutRoot_Loaded">

        <Grid>

            <Grid.ColumnDefinitions>

                <ColumnDefinition Width="*"/>

                <ColumnDefinition Width="Auto"/>

            </Grid.ColumnDefinitions>

            <diagrams:DiagramComponent x:Name="diagramComponent"/>

            <Grid Grid.Column="1" Background="White">

                <StackPanel Margin="5">

                    <Button Margin="5" Content="Save diagram" FontWeight="Bold" Click="Save_Click"/>

                    <Button Margin="5" Content="Load diagram" FontWeight="Bold" Click="Load_Click"/>

                </StackPanel>

            </Grid>

        </Grid>

    </Grid>

</UserControl>

Please, take a look at the following line:

<Button Margin="5" Content="Save diagram" FontWeight="Bold" Click="Save_Click"/>

 //in this line we add the Save diagram button

Step 4

You need to add ready diagrams in your project. In order to do this right-click on Serialization, select Add, then select New Folder and name it Diagrams.



It should look like this:



Then right-click on this folder, select Add -> Existing Item…



Then select grl files, that you’ve created earlier or grl files from the samples. Then click on Add. In this example we add Factorial.grl.



Your Solution Explorer should look like this:



Then open Properties of added diagrams.



Select BuildAction, then Resource.



Repeat these steps with other added diagrams.

Step 5

Open the MainPage.xaml.cs source code and add the following code.

    public partial class MainPage : UserControl

    {

        public MainPage()

        {

            InitializeComponent();

        }

        private void Load_Click(object sender, RoutedEventArgs e)

        {

            OpenFileDialog dialog = new OpenFileDialog();

            dialog.Filter = "Diagram files|*.grl";

            if ((bool)dialog.ShowDialog())

            {

                try

                {

                    using (var stream = dialog.File.OpenRead())

                    {

                        var s = dialog.File.Name;

                        diagramComponent.OpenDiagram(stream, dialog.File.Name);

                    }

                }

                catch

                {

                    MessageBox.Show("Can't open document");

                }

            }

        }

        private void Save_Click(object sender, RoutedEventArgs e)

        {

            var serializationService = diagramComponent.Model.Container.Resolve<SerializationService>();

            if (diagramComponent.Document != null)

            {

                SaveFileDialog dialog = new SaveFileDialog();

                dialog.DefaultExt = "grl";

                dialog.Filter = "Diagram files|*.grl";

                if ((bool)dialog.ShowDialog())

                {

                    using (var stream = dialog.OpenFile())

                    {

                        serializationService.WriteToPackage(diagramComponent.Document, stream);

                    }

                }

            }

        }

        private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)

        {

            using (var stream = ResourceHelper.LoadStream(Assembly.GetExecutingAssembly(), "Diagrams/Factorial.grl"))

            {

                diagramComponent.OpenDiagram(stream, "Factorial algorithm");

            }

        }

    }

Please, take a look at the following lines:

            using (var stream = ResourceHelper.LoadStream(Assembly.GetExecutingAssembly(), "Diagrams/Factorial.grl"))

            {

                diagramComponent.OpenDiagram(stream, "Factorial algorithm");

            }

 //in this lines we load the file from the resources to stream, and then open it

        private void Load_Click(object sender, RoutedEventArgs e)

        {

            OpenFileDialog dialog = new OpenFileDialog();

            dialog.Filter = "Diagram files|*.grl";

            if ((bool)dialog.ShowDialog())

            {

                try

                {

                    using (var stream = dialog.File.OpenRead())

                    {

                        var s = dialog.File.Name;

                        diagramComponent.OpenDiagram(stream, dialog.File.Name);

                    }

                }

                catch

                {

                    MessageBox.Show("Can't open document");

                }

            }

        }

//in this lines we open the dialog, then load the file to stream, and then open it

Step 6

Launch application by clicking the “Start Debugging”  button on the main Visual Studio toolbar. 




The application will open in the browser.





Add Feedback