aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Core/Components
diff options
context:
space:
mode:
authorRoy Ben Shabat <Roy.mail.net@gmail.com>2018-07-07 15:02:20 +0300
committerRoy Ben Shabat <Roy.mail.net@gmail.com>2018-07-07 15:02:20 +0300
commit4ca8cee91fb46977b75e8329c18d9b6a4654b12e (patch)
treec2a0473f30d9302f4b8eea21f90106d24ba23461 /Software/Visual_Studio/Tango.Core/Components
parentbed649c7492655c0137237c910b200e053bfa119 (diff)
downloadTango-4ca8cee91fb46977b75e8329c18d9b6a4654b12e.tar.gz
Tango-4ca8cee91fb46977b75e8329c18d9b6a4654b12e.zip
Working on PPC jobs loading performance...
Diffstat (limited to 'Software/Visual_Studio/Tango.Core/Components')
-rw-r--r--Software/Visual_Studio/Tango.Core/Components/FrameworkElementSerializer.cs124
1 files changed, 124 insertions, 0 deletions
diff --git a/Software/Visual_Studio/Tango.Core/Components/FrameworkElementSerializer.cs b/Software/Visual_Studio/Tango.Core/Components/FrameworkElementSerializer.cs
new file mode 100644
index 000000000..8f0c6a266
--- /dev/null
+++ b/Software/Visual_Studio/Tango.Core/Components/FrameworkElementSerializer.cs
@@ -0,0 +1,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;
+ }
+ }
+}