├── .gitignore
├── Demo app
├── Demo app.csproj
├── Program.cs
└── Properties
│ └── AssemblyInfo.cs
├── XAML conversion.sln
└── XAML conversion
├── Parsers
├── BindingParser.cs
├── ObjectParser.cs
├── ParserBase.cs
├── PropertyCollectionParser.cs
├── PropertyObjectParser.cs
├── PropertyParser.cs
├── RootObjectParser.cs
└── XamlParser.cs
├── Properties
└── AssemblyInfo.cs
├── XAML conversion.csproj
├── XamlConvertor.cs
└── XamlSchemaContextWithDefault.cs
/.gitignore:
--------------------------------------------------------------------------------
1 | *.suo
2 | *.user
3 | bin/
4 | obj/
5 | _ReSharper*
6 |
--------------------------------------------------------------------------------
/Demo app/Demo app.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Debug
5 | x86
6 | 8.0.30703
7 | 2.0
8 | {F905D677-17DD-4759-A194-7B7482343CEA}
9 | Exe
10 | Properties
11 | Demo_app
12 | Demo app
13 | v4.0
14 | Client
15 | 512
16 |
17 |
18 | x86
19 | true
20 | full
21 | false
22 | bin\Debug\
23 | DEBUG;TRACE
24 | prompt
25 | 4
26 |
27 |
28 | x86
29 | pdbonly
30 | true
31 | bin\Release\
32 | TRACE
33 | prompt
34 | 4
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 | {CE11B1F5-1289-4197-B1E9-816D9B040785}
54 | XAML conversion
55 |
56 |
57 |
58 |
65 |
--------------------------------------------------------------------------------
/Demo app/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Windows.Controls;
3 | using XamlConversion;
4 |
5 | namespace Demo_app
6 | {
7 | class Program
8 | {
9 | static void Main()
10 | {
11 | string xaml = @"
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 | ";
25 | Console.WriteLine(new XamlConvertor().ConvertToString(xaml));
26 |
27 | xaml = @"";
28 | Console.WriteLine(new XamlConvertor().ConvertToString(xaml));
29 | }
30 | }
31 |
32 | class CustomView : ViewBase
33 | {}
34 | }
--------------------------------------------------------------------------------
/Demo app/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("Demo app")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("Microsoft")]
12 | [assembly: AssemblyProduct("Demo app")]
13 | [assembly: AssemblyCopyright("Copyright © Microsoft 2011")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("0c54bee4-24ef-407f-abbb-57e31da0e2f7")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/XAML conversion.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 11.00
3 | # Visual Studio 2010
4 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "XAML conversion", "XAML conversion\XAML conversion.csproj", "{CE11B1F5-1289-4197-B1E9-816D9B040785}"
5 | EndProject
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Demo app", "Demo app\Demo app.csproj", "{F905D677-17DD-4759-A194-7B7482343CEA}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|Any CPU = Debug|Any CPU
11 | Debug|Mixed Platforms = Debug|Mixed Platforms
12 | Debug|x86 = Debug|x86
13 | Release|Any CPU = Release|Any CPU
14 | Release|Mixed Platforms = Release|Mixed Platforms
15 | Release|x86 = Release|x86
16 | EndGlobalSection
17 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
18 | {CE11B1F5-1289-4197-B1E9-816D9B040785}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
19 | {CE11B1F5-1289-4197-B1E9-816D9B040785}.Debug|Any CPU.Build.0 = Debug|Any CPU
20 | {CE11B1F5-1289-4197-B1E9-816D9B040785}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
21 | {CE11B1F5-1289-4197-B1E9-816D9B040785}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
22 | {CE11B1F5-1289-4197-B1E9-816D9B040785}.Debug|x86.ActiveCfg = Debug|Any CPU
23 | {CE11B1F5-1289-4197-B1E9-816D9B040785}.Release|Any CPU.ActiveCfg = Release|Any CPU
24 | {CE11B1F5-1289-4197-B1E9-816D9B040785}.Release|Any CPU.Build.0 = Release|Any CPU
25 | {CE11B1F5-1289-4197-B1E9-816D9B040785}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
26 | {CE11B1F5-1289-4197-B1E9-816D9B040785}.Release|Mixed Platforms.Build.0 = Release|Any CPU
27 | {CE11B1F5-1289-4197-B1E9-816D9B040785}.Release|x86.ActiveCfg = Release|Any CPU
28 | {F905D677-17DD-4759-A194-7B7482343CEA}.Debug|Any CPU.ActiveCfg = Debug|x86
29 | {F905D677-17DD-4759-A194-7B7482343CEA}.Debug|Mixed Platforms.ActiveCfg = Debug|x86
30 | {F905D677-17DD-4759-A194-7B7482343CEA}.Debug|Mixed Platforms.Build.0 = Debug|x86
31 | {F905D677-17DD-4759-A194-7B7482343CEA}.Debug|x86.ActiveCfg = Debug|x86
32 | {F905D677-17DD-4759-A194-7B7482343CEA}.Debug|x86.Build.0 = Debug|x86
33 | {F905D677-17DD-4759-A194-7B7482343CEA}.Release|Any CPU.ActiveCfg = Release|x86
34 | {F905D677-17DD-4759-A194-7B7482343CEA}.Release|Mixed Platforms.ActiveCfg = Release|x86
35 | {F905D677-17DD-4759-A194-7B7482343CEA}.Release|Mixed Platforms.Build.0 = Release|x86
36 | {F905D677-17DD-4759-A194-7B7482343CEA}.Release|x86.ActiveCfg = Release|x86
37 | {F905D677-17DD-4759-A194-7B7482343CEA}.Release|x86.Build.0 = Release|x86
38 | EndGlobalSection
39 | GlobalSection(SolutionProperties) = preSolution
40 | HideSolutionNode = FALSE
41 | EndGlobalSection
42 | EndGlobal
43 |
--------------------------------------------------------------------------------
/XAML conversion/Parsers/BindingParser.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Text.RegularExpressions;
3 |
4 | namespace XamlConversion.Parsers
5 | {
6 | class BindingParser : ParserBase
7 | {
8 | public BindingParser(XamlConvertor.State state)
9 | : base(state)
10 | {}
11 |
12 | static readonly Regex BindingRegex = new Regex(@"\{([\w]+)(\s+\w+=\w+)*\}");
13 |
14 | static readonly Regex BindingPropertyRegex = new Regex(@"(\w+)=(\w+)");
15 |
16 | public string Parse(string text)
17 | {
18 | var match = BindingRegex.Match(text);
19 |
20 | if (!match.Success)
21 | throw new InvalidOperationException();
22 |
23 | var type = GetTypeFromXName(match.Groups[1].Value);
24 |
25 | string variableName = CreateObject(type, type.Name);
26 | foreach (Capture capture in match.Groups[2].Captures)
27 | {
28 | var propertyMatch = BindingPropertyRegex.Match(capture.Value);
29 | if (!propertyMatch.Success)
30 | throw new InvalidOperationException();
31 | SetProperty(variableName, type, propertyMatch.Groups[1].Value, propertyMatch.Groups[2].Value);
32 | }
33 |
34 | return variableName;
35 | }
36 | }
37 | }
--------------------------------------------------------------------------------
/XAML conversion/Parsers/ObjectParser.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Xml.Linq;
3 |
4 | namespace XamlConversion.Parsers
5 | {
6 | class ObjectParser : XamlParser
7 | {
8 | public ObjectParser(XamlConvertor.State state)
9 | : base(state)
10 | {}
11 |
12 | public string VariableName { get; protected set; }
13 |
14 | public Type Type { get; protected set; }
15 |
16 | protected override void ParseName(XName name)
17 | {
18 | Type = GetTypeFromXName(name);
19 |
20 | VariableName = CreateObject(Type, name.LocalName);
21 | }
22 |
23 | protected override void ParseAttribute(XAttribute attribute)
24 | {
25 | if (attribute.IsNamespaceDeclaration)
26 | return;
27 |
28 | var propertyName = attribute.Name.LocalName;
29 | SetProperty(VariableName, Type, propertyName, attribute.Value);
30 | }
31 |
32 | protected override void ParseElement(XElement element)
33 | {
34 | // is it a property?
35 | if (element.Name.LocalName.Contains("."))
36 | {
37 | var propertyParser = new PropertyParser(State, this);
38 | propertyParser.Parse(element);
39 | }
40 | else
41 | {
42 | throw new NotImplementedException();
43 | }
44 | }
45 |
46 | protected override void ParseEnd()
47 | {}
48 | }
49 | }
--------------------------------------------------------------------------------
/XAML conversion/Parsers/ParserBase.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.CodeDom;
3 | using System.ComponentModel;
4 | using System.Globalization;
5 | using System.Windows.Data;
6 | using System.Xaml.Schema;
7 | using System.Xml.Linq;
8 |
9 | namespace XamlConversion.Parsers
10 | {
11 | abstract class ParserBase
12 | {
13 | protected XamlConvertor.State State { get; set; }
14 |
15 | protected ParserBase(XamlConvertor.State state)
16 | {
17 | State = state;
18 | }
19 |
20 | protected string CreateObject(Type type, string proposedName)
21 | {
22 | var variableName = State.GetVariableName(proposedName);
23 | var variableDeclaration = new CodeVariableDeclarationStatement(
24 | type.Name, variableName, new CodeObjectCreateExpression(type.Name));
25 | State.AddStatement(variableDeclaration);
26 | return variableName;
27 | }
28 |
29 | protected static Type GetTypeFromXName(XName xName)
30 | {
31 | string ns = xName.Namespace.NamespaceName;
32 | if (string.IsNullOrEmpty(ns))
33 | ns = "http://schemas.microsoft.com/netfx/2007/xaml/presentation";
34 | var xamlSchemaContext = new XamlSchemaContextWithDefault();
35 | return xamlSchemaContext.GetXamlType(new XamlTypeName(ns, xName.LocalName)).UnderlyingType;
36 | }
37 |
38 | protected static Type GetPropertyType(string name, Type type)
39 | {
40 | return type.GetProperty(name).PropertyType;
41 | }
42 |
43 | protected CodeExpression ConvertTo(string value, Type type)
44 | {
45 | var valueExpression = new CodePrimitiveExpression(value);
46 |
47 | var converter = TypeDescriptor.GetConverter(type);
48 |
49 | if (type == typeof(string) || type == typeof(object))
50 | return valueExpression;
51 |
52 | if (type == typeof(double))
53 | return new CodePrimitiveExpression(double.Parse(value, CultureInfo.InvariantCulture));
54 |
55 | if (type == typeof(BindingBase))
56 | {
57 | var bindingParser = new BindingParser(State);
58 | var bindingVariableName = bindingParser.Parse(value);
59 | return new CodeVariableReferenceExpression(bindingVariableName);
60 | }
61 |
62 | // there is no conversion availabe, the generated code won't compile, but there is nothing we can do about that
63 | if (converter == null)
64 | return valueExpression;
65 |
66 | var conversion = new CodeCastExpression(
67 | type.Name,
68 | new CodeMethodInvokeExpression(
69 | new CodeMethodInvokeExpression(new CodeTypeReferenceExpression("TypeDescriptor"), "GetConverter",
70 | new CodeTypeOfExpression(type.Name)), "ConvertFromInvariantString",
71 | new CodePrimitiveExpression(value)));
72 |
73 | return conversion;
74 | }
75 |
76 | protected void SetProperty(string variableName, Type variableType, string propertyName, string value)
77 | {
78 | var left = new CodePropertyReferenceExpression(
79 | new CodeVariableReferenceExpression(variableName), propertyName);
80 | var right = ConvertTo(value, GetPropertyType(propertyName, variableType));
81 | var assignment = new CodeAssignStatement(left, right);
82 | State.AddStatement(assignment);
83 | }
84 | }
85 | }
--------------------------------------------------------------------------------
/XAML conversion/Parsers/PropertyCollectionParser.cs:
--------------------------------------------------------------------------------
1 | using System.CodeDom;
2 | using System.Xml.Linq;
3 |
4 | namespace XamlConversion.Parsers
5 | {
6 | class PropertyCollectionParser : PropertyParser
7 | {
8 | public PropertyCollectionParser(XamlConvertor.State state, ObjectParser parent)
9 | : base(state, parent)
10 | {}
11 |
12 | protected override void ParseName(XName name)
13 | {
14 | Name = name.LocalName.Split('.')[1];
15 | Type = GetPropertyType(Name, Parent.Type);
16 | }
17 |
18 | protected override void ParseElement(XElement element)
19 | {
20 | var objectParser = new ObjectParser(State);
21 | objectParser.Parse(element);
22 | var addExpression =
23 | new CodeMethodInvokeExpression(
24 | new CodePropertyReferenceExpression(new CodeVariableReferenceExpression(Parent.VariableName), Name),
25 | "Add", new CodeVariableReferenceExpression(objectParser.VariableName));
26 | State.AddStatement(new CodeExpressionStatement(addExpression));
27 | }
28 |
29 | protected override void ParseEnd()
30 | {}
31 | }
32 | }
--------------------------------------------------------------------------------
/XAML conversion/Parsers/PropertyObjectParser.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.CodeDom;
3 | using System.Xml.Linq;
4 |
5 | namespace XamlConversion.Parsers
6 | {
7 | class PropertyObjectParser : PropertyParser
8 | {
9 | private bool m_firstElement = true;
10 |
11 | public PropertyObjectParser(XamlConvertor.State state, ObjectParser parent)
12 | : base(state, parent)
13 | {}
14 |
15 | protected override void ParseName(XName name)
16 | {
17 | Name = name.LocalName.Split('.')[1];
18 | Type = GetPropertyType(Name, Parent.Type);
19 | }
20 |
21 | protected override void ParseElement(XElement element)
22 | {
23 | if (!m_firstElement)
24 | throw new InvalidOperationException();
25 |
26 | var objectParser = new ObjectParser(State);
27 | objectParser.Parse(element);
28 |
29 | var left = new CodePropertyReferenceExpression(
30 | new CodeVariableReferenceExpression(Parent.VariableName), Name);
31 | var right = new CodeVariableReferenceExpression(objectParser.VariableName);
32 | var assignment = new CodeAssignStatement(left, right);
33 | State.AddStatement(assignment);
34 | }
35 |
36 | protected override void ParseEnd()
37 | {}
38 | }
39 | }
--------------------------------------------------------------------------------
/XAML conversion/Parsers/PropertyParser.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections;
3 | using System.Xml.Linq;
4 |
5 | namespace XamlConversion.Parsers
6 | {
7 | class PropertyParser : XamlParser
8 | {
9 | public string Name { get; protected set; }
10 |
11 | public Type Type { get; protected set; }
12 |
13 | protected ObjectParser Parent { get; private set; }
14 |
15 | private PropertyParser m_child;
16 |
17 | public PropertyParser(XamlConvertor.State state, ObjectParser parent)
18 | : base(state)
19 | {
20 | Parent = parent;
21 | }
22 |
23 | protected override void ParseName(XName name)
24 | {
25 | Name = name.LocalName.Split('.')[1];
26 | Type = GetPropertyType(Name, Parent.Type);
27 | // is it a collection?
28 | if (typeof(IEnumerable).IsAssignableFrom(Type) && Type != typeof(string))
29 | m_child = new PropertyCollectionParser(State, Parent);
30 | else
31 | m_child = new PropertyObjectParser(State, Parent);
32 | m_child.ParseName(name);
33 | }
34 |
35 | protected override void ParseAttribute(XAttribute attribute)
36 | {
37 | throw new InvalidOperationException();
38 | }
39 |
40 | protected override void ParseElement(XElement element)
41 | {
42 | m_child.ParseElement(element);
43 | }
44 |
45 | protected override void ParseEnd()
46 | {
47 | m_child.ParseEnd();
48 | }
49 | }
50 | }
--------------------------------------------------------------------------------
/XAML conversion/Parsers/RootObjectParser.cs:
--------------------------------------------------------------------------------
1 | using System.CodeDom;
2 |
3 | namespace XamlConversion.Parsers
4 | {
5 | class RootObjectParser : ObjectParser
6 | {
7 | public RootObjectParser(XamlConvertor.State state)
8 | : base(state)
9 | {}
10 |
11 | protected override void ParseEnd()
12 | {
13 | var returnStatement = new CodeMethodReturnStatement(new CodeVariableReferenceExpression(VariableName));
14 | State.AddStatement(returnStatement);
15 | State.SetReturnType(Type);
16 | }
17 | }
18 | }
--------------------------------------------------------------------------------
/XAML conversion/Parsers/XamlParser.cs:
--------------------------------------------------------------------------------
1 | using System.Xml.Linq;
2 |
3 | namespace XamlConversion.Parsers
4 | {
5 | abstract class XamlParser : ParserBase
6 | {
7 | protected XamlParser(XamlConvertor.State state)
8 | : base(state)
9 | {}
10 |
11 | public void Parse(XElement element)
12 | {
13 | ParseName(element.Name);
14 |
15 | foreach (var attribute in element.Attributes())
16 | ParseAttribute(attribute);
17 |
18 | foreach (var childElement in element.Elements())
19 | ParseElement(childElement);
20 |
21 | ParseEnd();
22 | }
23 |
24 | protected abstract void ParseName(XName name);
25 |
26 | protected abstract void ParseAttribute(XAttribute attribute);
27 |
28 | protected abstract void ParseElement(XElement element);
29 |
30 | protected abstract void ParseEnd();
31 | }
32 | }
--------------------------------------------------------------------------------
/XAML conversion/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("XAML conversion")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("Microsoft")]
12 | [assembly: AssemblyProduct("XAML conversion")]
13 | [assembly: AssemblyCopyright("Copyright © Microsoft 2011")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("6cf06685-d9c5-4a64-9fe5-a61b7d5a0aeb")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/XAML conversion/XAML conversion.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Debug
5 | AnyCPU
6 | 8.0.30703
7 | 2.0
8 | {CE11B1F5-1289-4197-B1E9-816D9B040785}
9 | Library
10 | Properties
11 | XamlConversion
12 | XAML conversion
13 | v4.0
14 | 512
15 |
16 |
17 | true
18 | full
19 | false
20 | bin\Debug\
21 | DEBUG;TRACE
22 | prompt
23 | 4
24 |
25 |
26 | pdbonly
27 | true
28 | bin\Release\
29 | TRACE
30 | prompt
31 | 4
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 |
65 |
--------------------------------------------------------------------------------
/XAML conversion/XamlConvertor.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.CodeDom;
3 | using System.CodeDom.Compiler;
4 | using System.Collections.Generic;
5 | using System.IO;
6 | using System.Xml.Linq;
7 | using Microsoft.CSharp;
8 | using XamlConversion.Parsers;
9 |
10 | namespace XamlConversion
11 | {
12 | public class XamlConvertor
13 | {
14 | internal class State
15 | {
16 | private readonly Dictionary m_variables = new Dictionary();
17 |
18 | public CodeMemberMethod Method { get; set; }
19 |
20 | public State()
21 | {
22 | Method = new CodeMemberMethod { Name = "Get" };
23 | }
24 |
25 | public void AddStatement(CodeStatement statement)
26 | {
27 | Method.Statements.Add(statement);
28 | }
29 |
30 | public void SetReturnType(Type returnType)
31 | {
32 | Method.ReturnType = new CodeTypeReference(returnType.Name);
33 | }
34 |
35 | public string GetVariableName(string originalName)
36 | {
37 | originalName = originalName.Substring(0, 1).ToLower() + originalName.Substring(1);
38 | if (m_variables.ContainsKey(originalName))
39 | {
40 | var number = ++m_variables[originalName];
41 | return originalName + number;
42 | }
43 | else
44 | {
45 | m_variables.Add(originalName, 1);
46 | return originalName;
47 | }
48 | }
49 | }
50 |
51 |
52 | public string ConvertToString(string xamlCode)
53 | {
54 | var dom = ConvertToDom(xamlCode);
55 | var compiler = new CSharpCodeProvider();
56 | var stringWriter = new StringWriter();
57 | compiler.GenerateCodeFromMember(dom, stringWriter, new CodeGeneratorOptions{BracingStyle = "C"});
58 | return stringWriter.ToString();
59 | }
60 |
61 | public CodeMemberMethod ConvertToDom(string xamlCode)
62 | {
63 | var state = new State();
64 |
65 | XElement root = XElement.Parse(xamlCode);
66 |
67 | new RootObjectParser(state).Parse(root);
68 |
69 | return state.Method;
70 | }
71 | }
72 | }
--------------------------------------------------------------------------------
/XAML conversion/XamlSchemaContextWithDefault.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 | using System.Reflection;
3 | using System.Xaml;
4 | using System.Linq;
5 |
6 | namespace XamlConversion
7 | {
8 | class XamlSchemaContextWithDefault : XamlSchemaContext
9 | {
10 | private readonly Assembly m_defaultAssembly;
11 |
12 | public XamlSchemaContextWithDefault() : this(Assembly.GetEntryAssembly())
13 | {}
14 |
15 | public XamlSchemaContextWithDefault(Assembly defaultAssembly)
16 | : base(GetReferenceAssemblies())
17 | {
18 | m_defaultAssembly = defaultAssembly;
19 | }
20 |
21 | static IEnumerable GetReferenceAssemblies()
22 | {
23 | return new[] { "WindowsBase", "PresentationCore", "PresentationFramework" }.Select(an => Assembly.LoadWithPartialName(an));
24 | }
25 |
26 | protected override Assembly OnAssemblyResolve(string assemblyName)
27 | {
28 | if (string.IsNullOrEmpty(assemblyName))
29 | return m_defaultAssembly;
30 |
31 | return base.OnAssemblyResolve(assemblyName);
32 | }
33 | }
34 | }
--------------------------------------------------------------------------------