aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Editors/BrushExtensions.cs
blob: 88ef997a6a2f9484e2d39ea5e2aba0c69dbe4f60 (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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Markup;
using System.Windows.Media;
using System.Xml;


internal static class BrushExtensions
{
    internal static String ConvertToString(this Brush brush)
    {
        XmlWriterSettings settings = new XmlWriterSettings();
        // You might want to wrap these in #if DEBUG's 
        settings.Indent = true;
        settings.NewLineOnAttributes = true;
        // this gets rid of the XML version 
        settings.ConformanceLevel = ConformanceLevel.Fragment;
        // buffer to a stringbuilder
        StringBuilder sb = new StringBuilder();
        XmlWriter writer = XmlWriter.Create(sb, settings);
        // Need moar documentation on the manager, plox MSDN
        XamlDesignerSerializationManager manager = new XamlDesignerSerializationManager(writer);
        manager.XamlWriterMode = XamlWriterMode.Expression;
        // its extremely rare for this to throw an exception
        XamlWriter.Save(brush, manager);

        return sb.ToString();
    }

    internal static Brush ToBrush(this String str)
    {
        XmlDocument doc = new XmlDocument();
        // may throw XmlException
        doc.LoadXml(str);
        // may throw XamlParseException
        return XamlReader.Load(new XmlNodeReader(doc)) as Brush;
    }
}