-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathResourceDictionaryGenerator.cs
More file actions
80 lines (70 loc) · 3.34 KB
/
ResourceDictionaryGenerator.cs
File metadata and controls
80 lines (70 loc) · 3.34 KB
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
using System;
using System.CodeDom;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace EmptyKeys.UserInterface.Generator
{
/// <summary>
/// Implements resource dictionary generator
/// </summary>
public class ResourceDictionaryGenerator
{
private int uniqueId;
/// <summary>
/// Generates the specified dictionary.
/// </summary>
/// <param name="dictionary">The dictionary.</param>
/// <param name="classType">Type of the class.</param>
/// <param name="initMethod">The initialize method.</param>
/// <param name="fieldReference">The field reference.</param>
/// <param name="element">The parent element.</param>
public void Generate(ResourceDictionary dictionary, CodeTypeDeclaration classType, CodeMemberMethod initMethod, CodeExpression fieldReference, FrameworkElement element = null)
{
foreach (var mergedDict in dictionary.MergedDictionaries)
{
string name = string.Empty;
if (mergedDict.Source.IsAbsoluteUri)
{
name = Path.GetFileNameWithoutExtension(mergedDict.Source.LocalPath);
}
else
{
name = Path.GetFileNameWithoutExtension(mergedDict.Source.OriginalString);
}
if (string.IsNullOrEmpty(name))
{
Console.WriteLine("Dictionary name not found.");
continue;
}
CodeMethodInvokeExpression addMergedDictionary = new CodeMethodInvokeExpression(
fieldReference, "MergedDictionaries.Add", new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(name), "Instance"));
initMethod.Statements.Add(addMergedDictionary);
}
ValueGenerator valueGenerator = new ValueGenerator();
List<object> keys = dictionary.Keys.Cast<object>().OrderBy(k => k.ToString()).ToList();
foreach (var resourceKey in keys)
{
object resourceValue = dictionary[resourceKey];
CodeComment comment = new CodeComment("Resource - [" + resourceKey.ToString() + "] " + resourceValue.GetType().Name);
initMethod.Statements.Add(new CodeCommentStatement(comment));
CodeExpression keyExpression = CodeComHelper.GetResourceKeyExpression(resourceKey);
string basename;
if (element != null) basename = element.Name + "_r";
else basename = "r";
CodeExpression valueExpression = valueGenerator.ProcessGenerators(classType, initMethod, resourceValue, basename + uniqueId, dictionary);
if (valueExpression != null)
{
CodeMethodInvokeExpression addResourceMethod = new CodeMethodInvokeExpression(fieldReference, "Add", keyExpression, valueExpression);
initMethod.Statements.Add(addResourceMethod);
}
uniqueId++;
}
}
}
}