JSEDLAK » WPF

Posts Tagged ‘WPF’

VS2010 is Looking Sexy

As Jason Zander shows on his blog, Visual Studio 2010 is looking gorgeous in its WPF kit. The added capabilities of the IDE and the power extensions now have will put this software above and beyond the rest for many years. If Microsoft still does one thing brilliantly, it is write great IDE software.

A Spark of Genius?

Knowing how frameworks are designed and developed definately has its benefits. Awhile ago I spent some time looking into how DependencyObject and DependencyProperty work in WPF. Essentially, many of the properties are not simple properties but rather facades for method invocations.

Fast forward to today when I was working on Versionator, an application I am writing to help me manage my many projects’ versions. The problem is that project information files (AssemblyInfo.cs) are not straight forward in terms of parsing. They are merely text files, awkward and malformed code files in fact. To be able to support many properties so easily, I came up with a scheme to parse, manage and save the projects’ properties in the file.

What I do is parse the properties, extracting the data and storing it in a dictionary as “old values.” At the same time I create a copy of the values in a “current values” dictionary. As new values come in, I channel them through properties, then Get and Set methods which access the “current values” dictionary. When it comes time to save I have to do some management of the data based on whether or not the property was empty, non-existant or is being changed to empty. It is simple to say that it would have taken me a lot longer if I had not taken a look at WPF. So was it a spark of genius? Or just plain old learnedness?

The Code (Beware of its massive length):

?View Code CSHARP
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
using System;
using System.Collections.Generic;
using System.IO;
 
namespace FocusedGames.Versionator.Data
{
    public class ProjectInfo
    {
        public enum Properties : ushort
        {
            Title = 0,
            Description = 1,
            Company = 2,
            Product = 3,
            Copyright = 4,
            Trademark = 5,
            AssemblyVersion = 6,
            FileVersion = 7,
            Guid = 8
        }
 
        private readonly string[] lookups = new [] { "AssemblyTitle", "AssemblyDescription", "AssemblyCompany", "AssemblyProduct", "AssemblyCopyright", "AssemblyTrademark", "AssemblyVersion", "AssemblyFileVersion", "Guid" };
        private readonly Dictionary<ushort, string> propertyValues = new Dictionary<ushort, string>();
        private readonly Dictionary<ushort, string> oldValues = new Dictionary<ushort, string>();
 
        public static ProjectInfo FromFile(string filename)
        {
            return new ProjectInfo(filename);
        }
 
        public ProjectInfo()
        {
            Array enumValues = Enum.GetValues(typeof (Properties));
 
            foreach(ushort prop in enumValues)
            {
                propertyValues.Add(prop, string.Empty);
                oldValues.Add(prop, string.Empty);
            }
        }
 
        public ProjectInfo(string filename)
            : this()
        {
            Load(filename);
        }
 
        #region Getting and Setting Project Properties
        protected void SetProperty(Properties prop, string value)
        {
            propertyValues[(ushort) prop] = value;
        }
 
        protected string GetProperty(Properties prop)
        {
            string newValue = propertyValues[(ushort) prop];
 
            if(string.IsNullOrEmpty(newValue))
                return oldValues[(ushort) prop];
 
            return newValue;
        }
        #endregion
 
        #region Loading and Saving
        public void Load(string filename)
        {
            Filename = filename;
 
            string[] lines = File.ReadAllLines(Filename);
 
            Array enumValues = Enum.GetValues(typeof (Properties));
 
            foreach(ushort prop in enumValues)
            {
                Load(lines, prop);
            }
        }
 
        private void Load(string[] lines, ushort lookup)
        {
            for(int i = 0; i < lines.Length; i++)
            {
                string currentLine = lines[i];
                string searchTerm = lookups[lookup];
 
                // Get the first index of the search term
                int firstIndex = currentLine.IndexOf(searchTerm);
 
                // The property wasn't found, continue
                // through the file.
                if(firstIndex == -1)
                    continue;
 
                // Now we need to extract the data out of the string!
                string data = currentLine.Substring(firstIndex + searchTerm.Length).TrimFront("\"").TrimBack("\"");
 
                oldValues[lookup] = data;
                propertyValues[lookup] = data;
            }
        }
 
        public void Save()
        {
            string[] lines = File.ReadAllLines(Filename);
 
            Array enumValues = Enum.GetValues(typeof(Properties));
 
            foreach (ushort prop in enumValues)
                Save(lines, prop);
 
            StreamWriter sw = new StreamWriter(Filename, false);
 
            for(int i = 0; i < lines.Length; i++)
                sw.WriteLine(lines[i]);
 
            sw.Close();
        }
 
        private void Save(string[] lines, ushort prop)
        {
            for (int i = 0; i < lines.Length; i++)
            {
                string currentLine = lines[i];
                string searchTerm = lookups[prop];
 
                // Get the first index of the search term
                int firstIndex = currentLine.IndexOf(searchTerm);
 
                // The property wasn't found, continue
                // through the file.
                if (firstIndex == -1)
                    continue;
 
                string oldValueLocal = oldValues[prop];
                string newValueLocal = propertyValues[prop];
 
                // If the old value didn't exist, we can adjust the 
                // replacement mechanism to remove the "" and add
                // them back in.
                if (string.IsNullOrEmpty(oldValueLocal))
                {
                    oldValueLocal = "\"\"";
                    newValueLocal = string.Format("\"{0}\"", newValueLocal);
                }
 
                // We also need to catch people emptying strings out.
                // To do this we adjust the saving mechanism the same way.
                if(string.IsNullOrEmpty(newValueLocal))
                {
                    oldValueLocal = string.Format("\"{0}\"", oldValueLocal);
                    newValueLocal = "\"\"";
                }
 
                // Replace the old value with the new value
                if (!string.IsNullOrEmpty(newValueLocal))
                        lines[i] = lines[i].Replace(oldValueLocal, newValueLocal);
            }
        }
        #endregion
 
        public string Filename { get; private set; }
 
        public string AssemblyVersion
        {
            get { return GetProperty(Properties.AssemblyVersion); }
            set { SetProperty(Properties.AssemblyVersion, value); }
        }
 
        public string FileVersion
        {
            get { return GetProperty(Properties.FileVersion); }
            set { SetProperty(Properties.FileVersion, value); }
        }
 
        public string Title 
        {
            get { return GetProperty(Properties.Title); }
            set { SetProperty(Properties.Title, value); }
        }
 
        public string Description
        {
            get { return GetProperty(Properties.Description); }
            set { SetProperty(Properties.Description, value); }
        }
 
        public string Company
        {
            get { return GetProperty(Properties.Company); }
            set { SetProperty(Properties.Company, value); }
        }
 
        public string Product 
        {
            get { return GetProperty(Properties.Product); }
            set { SetProperty(Properties.Product, value); }
        }
 
        public string Copyright
        {
            get { return GetProperty(Properties.Copyright); }
            set { SetProperty(Properties.Copyright, value); }
        }
 
        public string Trademark
        {
            get { return GetProperty(Properties.Trademark); }
            set { SetProperty(Properties.Trademark, value); }
        }
 
        public string Guid
        {
            get { return GetProperty(Properties.Guid); }
            set { SetProperty(Properties.Guid, value); }
        }
    }
}

P.S. The code is not done, is not even close to being efficient or functionally complete. But it works!

Restoring LocalUserAppDataPath in WPF

With the arrival of .NET 3.5 and consequently WPF applications, Microsoft has done away with the old Application WinForms class. No longer can you call Application.LocalUserAppDataPath to get where the user’s files are stored. To restore this functionality as well as add a bit more, I have tapped into an uncommon resource: extension methods. Let’s dig a little deeper, shall we?

Read the rest of this entry »