What a great way to start the new year! On New Year’s Day, Microsoft announced the recipients of the Microsoft Most Valuable Professional (MVP) award and I was fortunate enough to be one of them! This is my first time receiving the MVP award and I am very honored to be recognized for my efforts in the Silverlight community! Thanks to everyone I’ve worked with over the past year, it has been fun! I’m looking forward to seeing everyone at the MVP Summit!
I don't really like making New Year Resolutions, because it seems like I always give up on them a couple weeks into the new year. But since I'm already doing most of these, I guess it doesn't matter what I call them. I hope this has been a great year for all of you and here's to a great 2009! See you next year!
My New Year Resolutions
Silverlight Contrib
The first 3 releases of Silverlight Contrib generated a lot of buzz in the community. I'm really excited about next year and hope to continue working to make this project useful to the community.
No Caffeine & No Soft Drinks
I gave up caffeine a couple weeks back. After a 3-day caffeine hang-over, I broke my addiction. The next step is to completely eliminate soft drinks from my diet. This has been a difficult process, but I think the worst part is over. It is amazing how much easier it is to sleep and wake-up in the morning.
If you are thinking about quitting soft drinks but haven't yet, you might want to read this.
Exercise every day for at least 30 minutes
I think this will be the hardest of all. I generally run about 9 or 10 miles a week, but I want to increase that to 13-15 miles a week. I really think having a treadmill or gym membership helps here. Especially during the fall and winter seasons.
Want to learn how to run the 5K in 30 minutes? Check this exercise plan out.
Continue Speaking Engagements
I made a focused effort to attend and speak at as many regional developer events as I possibly could this year. I had a great time and met a lot of new people in the process. Next year, I hope to have just as many speaking engagements!
Continue Blogging
Sometimes it's hard to find time to write on my blog, but it has been an excellent way to express my ideas and record my findings. Next year, I plan to continue blogging.
Read, Read, Read
Reading is my favorite hobby that I never have time for. I've promised myself that I will make more time for reading. Here are a few books that I am working on at the moment:
Steve Krug: Don't Make Me Think

Martin Fowler: Patterns of Enterprise Application Architecture
Thomas E. Kida: Don't Believe Everything You Think: The 6 Basic Mistakes We Make in Thinking

Silverlight Contrib Alpha 3 has been released! This release builds upon the Alpha 2 release with minor enhancements in many areas. As always, we are requesting your feedback so we can make the product better! Also, if you think you could help make Silverlight Contrib better and are serious about helping, we would love to hear from you! We need all the help we can get! Thanks!
Silverlight Contrib 2008.0.0 - Alpha 3 Download
Changes since the last release:
- Added API Documentation (CHM)
- Added Dynamic Imaging Support
- Editable Image
- BmpDecoder
- PngEncoder
- Added new IValueConverters
- HtmlDecodeConverter
- StringFormatConverter
- Animation Extension Methods
- Animate
- AnimateDouble
- FindStoryboard
- Color Picker Control is now resizable.
- Gauge Control supports optional pop-up progress indicator.
- Added Read-Only property for Star Selector control.
- Added ReSharper Templates
- Dependency Property Generator
- VSM Block Generator
- Silverlight Control Template
- Generic.xaml template.
When building a Silverlight control, in some situations, you need the visual elements of the control to scale according to the size of the control. Sometimes, this is easy because you can simply leave the height and width dimensions unspecified for the visual elements and they will fill to consume the available space in the parent container. But in more complex situations, you need to be able to specify the dimensions of the visual elements in your control.
The color picker control illustrated above has different parts, each of which should maintain the appropriate relative size. But how do we maintain the aspect ratio and relative sizes of the visual elements in the control when the control is resized? Furthermore, the font size used for displaying the selected color is set to 11px, how do we make the font size grow and shrink with the control?
One possibility is to use a ScaleTransform on the root element of you control. When the control size changes, a scale transform is applied to “fill” the control’s boundaries. In fact, this is how the ViewBox control (from the Silverlight Toolkit) works. I’ve distilled a large portion of the logic from the ViewBox down to something that works for my scenario. Hopefully you will find this useful as well!
Step 1: Obtain a reference to the root element and apply a Scale Transform object to the root element.
public override void OnApplyTemplate()
{
m_rootElement = GetTemplateChild("RootElement") as Panel;
m_rootElementScale = new ScaleTransform();
m_rootElement.RenderTransform = m_rootElementScale;
base.OnApplyTemplate();
}
Step 2: In the MeasureOverride call for the control, determine the root element’s desired size and then find the appropriate scale factor to make the control fill the available size.
protected override Size MeasureOverride(Size availableSize)
{
Size size = new Size();
if (m_rootElement != null)
{
m_rootElement.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
Size desiredSize = m_rootElement.DesiredSize;
Size scaleSize = CalculateScaleSize(availableSize, desiredSize);
size.Width = scaleSize.Width * desiredSize.Width;
size.Height = scaleSize.Height * desiredSize.Height;
}
return size;
}
private Size CalculateScaleSize(Size availableSize, Size desiredSize)
{
double scaleWidth = 1;
double scaleHeight = 1;
if (!double.IsPositiveInfinity(availableSize.Width) &&
!double.IsPositiveInfinity(availableSize.Height))
{
scaleWidth = desiredSize.Width == 0 ? 0 : (availableSize.Width / desiredSize.Width);
scaleHeight = desiredSize.Height == 0 ? 0 : (availableSize.Height / desiredSize.Height);
}
return new Size(scaleWidth, scaleHeight);
}
Step 3: In the ArrangeOverride call, use the calculated scale size to adjust the render transformation on the root element and the position the root element.
protected override Size ArrangeOverride(Size finalSize)
{
if (m_rootElement != null)
{
Size desiredSize = m_rootElement.DesiredSize;
Size scaleSize = CalculateScaleSize(finalSize, desiredSize);
m_rootElementScale.ScaleX = scaleSize.Width;
m_rootElementScale.ScaleY = scaleSize.Height;
Rect originalPosition = new Rect(0, 0, desiredSize.Width, desiredSize.Height);
m_rootElement.Arrange(originalPosition);
finalSize.Width = scaleSize.Width * desiredSize.Width;
finalSize.Height = scaleSize.Height * desiredSize.Height;
}
return finalSize;
}
With this logic in place, we can now resize the control with the correct scaling behavior in place. As you can see in the screenshot below, the control now scales gracefully!

ReSharper is a great tool for developer productivity. I absolutely can't live without it! If you haven't already tried ReSharper, you should do yourself a favor and go download it now. It takes a while to get used to, but once you grok the features (namely the keyboard shortcuts), you will see how powerful this tool really is.
Ok enough with the testimonials, how can you take advantage of ReSharper when working on Silverlight projects? If you have ever used Resharper, you may know that it has a great feature called Templates. Templates give you the ability to quickly generate code that you would generally need to write by hand. There are three different types of templates in ReSharper:
- Live Templates - Allow you to generate code within the context of an existing file.
- Surround Templates - Allow you to "surround" selected code with a pre-defined template.
- File Templates - Allow you to quickly generate an entire file from a pre-defined template.
I've built 4 templates that you can use in your Silverlight endeavors. Below you will find a short description of each:
Dependency Property Generation
Let's face it, it takes a lot of code to declare a dependency property. I have created a Live Template that will automate almost all of the work for you. As illustrated in the screen capture below, you simply type DependProp in the editor, supply a property name and type, then hit tab and you have a fully defined dependency property!
Visual State Manager Block Template
The Visual State Manager Block Template is similar in function to the Dependency Property template, but this template will build a VisualStateManager declaration within the context of an XAML file. To use the template, simply type vsm while editing an XML/XAML file. The usage for this template is very similar to the DependProp template.
Generic.xaml File Template
As a Silverlight control developer, you may find it annoying to have to define the generic.xaml file over and over again. Using a File Template, we can quickly build standard declaration and ReSharper will help you fill out the rest. To generate a new Generic.xaml file, press ALT+R+N+G, enter a name for the template and then click OK.
Control File Template
The Control File Template will construct a basic class that inherits from Control and includes the DefaultStyleKey in the constructor. The usage for this template is the same as the Generic.xaml file template above.
Installing the Templates
To import these templates, select the File Templates tab in the ReSharper Templates Explorer. Click the import button and select the SilverlightFileTemplates.xml file,then click ok.
Importing the Live Templates is very similar. Select the Live Templates tab in the ReSharper Templates Explorer. Click the import button select the SilverlightLiveTemplates.xml file, then click Ok.
Conclusion and Download
I’d be interested in hearing any suggestions for these templates, Also, if you have built any ReSharper templates that you find useful, please let me know! My goal is to find and add the most useful ReSharper templates and add them to Silverlight Contrib.
In the meantime, you can download them here:
Download Resharper Templates
When building a custom control in Silverlight 2, the OnApplyTemplate method gives you the opportunity to “wire-up” logical instances of elements in your control. When a control is loaded, it is important to realize that in Silverlight 2, this method is not triggered very early with respect to other events. In other words, you could very easily write code that fails just because of where you call it. Understanding the control’s life cycle will help in avoiding these situations.
At the time this was written, the MSDN documentation stated:
“The timing of the Loaded event in Silverlight differs from the timing of the FrameworkElement.Loaded event in WPF. Specifically, the WPF Loaded event occurs after the template is applied. In Silverlight, the Loaded event is not guaranteed to occur after the template is applied.”
After running a quick test, it is easy to see that this is true. My simple test shows that the OnApplyTemplate call fired after both the Control’s Loaded Event and the Page Loaded event.
Note: Dave Relyea has a very comprehensive post with a table outlining the control lifecycle which confirms my test :-)
Without too much trouble, you can force the OnApplyTemplate call to fire and consequently change the order of events with a simple work-around:
public class MyControl : Control
{
public MyControl()
{
this.DefaultStyleKey = typeof(MyControl);
this.Loaded += new RoutedEventHandler(MyControl_Loaded);
}
void MyControl_Loaded(object sender, RoutedEventArgs e)
{
this.ApplyTemplate(); // Synchronous call ...
}
public override void OnApplyTemplate()
{
...
}
...
By calling ApplyTemplate in the Loaded event handler, we effectively change the event order to the following:
Now, any code in the Control or Page Loaded event handlers that is dependent upon the template being applied will fire correctly.
Open Spaces 
Tuesday, November 11th, 2008
Open Spaces
Topic: Open Spaces - Bring your questions and ideas!
Tuesday, November 11th, 2008, is the next meeting of the Pee Dee Area .NET User Group.
Open Spaces is the chance for you to discuss topics with your peers. Any developer topic is fair game and welcome! This is your chance to gain valuable insight from your peers, so come equipped with your ideas! This meeting is a general open conversation and there will be no presenters. See you there!
Here is the tentative schedule:
6:00 PM - 6:20 PM Socializing / Free Dinner
6:20 PM - 6:30 PM Introduction, Sponsor Time, and News.
6:30 PM - 8:00 PM Open Spaces
The MSDN Southern Fried Roadshow is coming to North Carolina and South Carolina in December!
These free events are a great way to keep up with new Microsoft technologies, so be sure to register for an even near you!
North Charleston, SC - Register
12/2/2008 9:00 AM - 3:00 PM ET
Wilmington, NC - Register
12/3/2008 9:00 AM - 3:00 PM ET
Raleigh, NC - Register
12/4/2008 9:00 AM - 3:00 PM ET
Charlotte, NC - Register
12/5/2008 9:00 AM - 3:00 PM ET
Event Description
The MSDN Southern Fried Roadshow is a 3/4 day, free developer event with a southern flair where you will learn about some of the latest developments in Microsoft technologies. For this edition we will cover a broad range of the latest Microsoft Technologies:
- Take a tour of Microsoft’s cloud computing platform and the services that make it easy to give your applications the most compelling experiences and features. Explore the journey a developer takes, from writing a service to launching that service in the cloud. Learn about the cloud services that enable developers to easily create or extend their applications and services.
- Microsoft Silverlight provides a powerful platform for building the next generation of rich interactive applications on the Internet. In this session, we take a look at the programming model and tools that developers and designers can leverage to build these true next-generation experiences for consumers and business, and demonstrate building a rich interactive application (RIA) using Silverlight and Microsoft .NET.
- ASP.NET is evolving a very rapid pace, come explore all the latest features of ASP.Net such as Dynamic Data, MVC, and even take a sneak peak at the upcoming features in ASP.NET 4.0! We’ll look at some new features such as taking control of your Control IDs, using the DynamicImage control, and ViewState management options. We’ll spend plenty of time talking about MVC and creating applications based on this framework, so if you’re wondering how to leverage MVC in your web applications, this talk is for you!
We will also feature local speakers from each of our stops delivering short talks at the beginning of the Roadshow. These Homegrown Nuggets will sure be tasty!
A few weeks ago in the Silverlight forums, a user asked how they could attach to the OnCompleted event of a Visual StateManager animation. At first, this sounds trivial, but the VisualStateManager does not directly expose a way to obtain a Storyboard. However, the VisualStateManager does return a list of the VisualStateGroups through the GetVisualStateGroups method. We can take advantage of this method to obtain a reference to the desired storyboard.
The FindStoryboard method defined in this helper class iterates over each of the VisualStateGroups until it finds a match, it then iterates over each Visual State in the group until it finds a match. The Storyboard for the matching visual state is then returned.
public class VisualStateManagerUtils
{
public static Storyboard FindStoryboard(FrameworkElement parent, string groupName, string stateName)
{
var vsgs = VisualStateManager.GetVisualStateGroups(parent);
foreach(VisualStateGroup vsg in vsgs)
{
if (vsg.Name != groupName)
continue;
foreach (VisualState vs in vsg.States)
{
if (vs.Name == stateName)
return vs.Storyboard;
}
}
return null;
}
}
Now, it’s a simple matter of calling our helper method and doing something useful with the Storyboard reference. For example, you could attach an event handler to the Completed event on the Storyboard.
public partial class Page : UserControl
{
public Page()
{
...
Storyboard sbHidden = VisualStateManagerUtils.FindStoryboard(LayoutRoot, "CommonStates", "Hidden");
sbHidden.Completed += new EventHandler(sbHidden_Completed);
}
void sbHidden_Completed(object sender, EventArgs e)
{
// Do something cool here. } ... ...
Update: had an excellent suggestion of making this method an extension method. The resulting syntax is much more concise and easier to use. With a few small changes, we can convert the helper method into an extension method.
public static class VisualStateManagerUtils
{
public static Storyboard FindStoryboard(this FrameworkElement parent, string groupName, string stateName)
{
var vsgs = VisualStateManager.GetVisualStateGroups(parent);
foreach (VisualStateGroup vsg in vsgs)
{
if (vsg.Name != groupName)
continue;
foreach (VisualState vs in vsg.States)
{
if (vs.Name == stateName)
return vs.Storyboard;
}
}
return null;
}
}
Now, it is much simpler to call the method
Storyboard sb = LayoutRoot.FindStoryboard("CommonStates", "Hidden");
As previously announced by Shawn Burke on his blog, the Silverlight Toolkit has just been released! Much like the AJAX Control Toolkit for ASP.NET, the Silverlight Control Toolkit Team will be introducing new controls and features which may eventually make it into the Silverlight platform. I like to think of it as an incubator for new controls in Silverlight. This is really cool because it gives the community the chance to use the controls much earlier and provide valuable feedback to help influence the product direction. Another added benefit to this is that the source code is hosted on CodePlex so you can look at the code (along with the Unit Tests) and get a better understanding of what is going on! It doesn't appear that the team will be accepting community contributions, but hey, that's what Silverlight Contrib is for!
Here is a list of the features for the first drop of the Silverlight Toolkit:
Preview Quality Band
- AutoCompleteBox
- NumericUpDown
- ViewBox
- Expander
- ImplicitStyleManager
- Charting
Stable Quality Band
- TreeView
- DockPanel
- WrapPanel
- Label
- HeaderedContentControl
- HeaderedItemsControl
Themes
- Expression Dark
- Expression Light
- Rainier Purple
- Rainier Orange
- Shiny Blue
- Shiny Red
Enjoy!