aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Core/Components/FrameworkElementSerializer.cs
blob: 8f0c6a2662236399c2324d9255968d2369e2f48f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;
using System.Windows.Markup;
using System.Xml;

namespace Tango.Core.Components
{
    public static class FrameworkElementSerializer
    {
        private class BindingConvertor : ExpressionConverter
        {
            public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
            {
                if (destinationType == typeof(MarkupExtension))
                    return true;
                else return false;
            }
            public override object ConvertTo(ITypeDescriptorContext context,
                                             System.Globalization.CultureInfo culture,
                                             object value, Type destinationType)
            {
                if (destinationType == typeof(MarkupExtension))
                {
                    BindingExpression bindingExpression = value as BindingExpression;
                    if (bindingExpression == null)
                        throw new Exception();
                    return bindingExpression.ParentBinding;
                }

                return base.ConvertTo(context, culture, value, destinationType);
            }

            public static void Register<T, TC>()
            {
                Attribute[] attr = new Attribute[1];
                TypeConverterAttribute vConv = new TypeConverterAttribute(typeof(TC));
                attr[0] = vConv;
                TypeDescriptor.AddAttributes(typeof(T), attr);
            }
        }

        static FrameworkElementSerializer()
        {
            BindingConvertor.Register<BindingExpression, BindingConvertor>();
        }

        /// <summary>
        /// Clones the specified element.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <returns></returns>
        public static FrameworkElement Clone(FrameworkElement element)
        {
            var sb = new StringBuilder();
            var writer = XmlWriter.Create(sb, new XmlWriterSettings
            {
                Indent = true,
                ConformanceLevel = ConformanceLevel.Fragment,
                OmitXmlDeclaration = true,
                NamespaceHandling = NamespaceHandling.OmitDuplicates,
            });
            var mgr = new XamlDesignerSerializationManager(writer);

            // HERE BE MAGIC!!!
            mgr.XamlWriterMode = XamlWriterMode.Expression;
            // THERE WERE MAGIC!!!

            XamlWriter.Save(element, mgr);
            String xaml = sb.ToString();

            StringReader stringReader = new StringReader(xaml);
            XmlReader xmlReader = XmlReader.Create(stringReader);
            FrameworkElement cloned = (FrameworkElement)XamlReader.Load(xmlReader) as FrameworkElement;
            return cloned;
        }

        /// <summary>
        /// Serializes the specified element.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <returns></returns>
        public static String Serialize(FrameworkElement element)
        {
            var sb = new StringBuilder();
            var writer = XmlWriter.Create(sb, new XmlWriterSettings
            {
                Indent = true,
                ConformanceLevel = ConformanceLevel.Fragment,
                OmitXmlDeclaration = true,
                NamespaceHandling = NamespaceHandling.OmitDuplicates,
            });
            var mgr = new XamlDesignerSerializationManager(writer);

            // HERE BE MAGIC!!!
            mgr.XamlWriterMode = XamlWriterMode.Expression;
            // THERE WERE MAGIC!!!

            XamlWriter.Save(element, mgr);
            String xaml = sb.ToString();

            return xaml;
        }

        /// <summary>
        /// Deserializes the specified xaml.
        /// </summary>
        /// <param name="xaml">The xaml.</param>
        /// <returns></returns>
        public static FrameworkElement Deserialize(String xaml)
        {
            StringReader stringReader = new StringReader(xaml);
            XmlReader xmlReader = XmlReader.Create(stringReader);
            FrameworkElement cloned = (FrameworkElement)XamlReader.Load(xmlReader) as FrameworkElement;
            return cloned;
        }
    }
}