Changing Generated Code in VS2008

Have you ever been dissatisfied with the code that is automatically generated by Visual Studio in response to various commands that you perform. For example, in VS2005, I got used to using the prop snippet to create properties. This snippet created a property declaration complete with a private backing variable. However, in VS2008, the prop snippet was modified to create a property declaration in the form of the automatic property declaration. That, to me, was annoying.

(Note to Microsoft: Please consider the existing snippets to be part of what you consider when looking at backwards compatibility. Create more snippets, sure. But please don’t change what the current snippets do…especially when the ‘better’ snippets are not actually better)

Or, consider the fact that, as more and more work is done in WPF, the need to ensure that business classes implement INotifyPropertyChanged grows. Adding or modifying a snippet to include the code to call PropertyChanged could be quite useful. It certainly has been for me.

Little known by most developers is the location for the snippets that Visual Studio provides. Or, what might be more interesting to intrepid developers, the snippets used by Visual Studio refactoring. All of these are found in %ProgramFiles%\Microsoft Visual Studio 9.0\VC#\Snippets\1033. The keyword snippets can be found in the Visual C# subdirectory, while the refactoring snippets can be found in the Refactoring subdirectory. Take a look at what’s there and you may be surprised how you can increase your productivity for mundane (and frequent) tasks.

“Fixing” the WPF Designer

If you use WPF on a regular basis, then I can take a good guess as the esteem in which you hold the designer. While it has it’s place, hard code WPF devs using XAML directly. It’s much each to get the result that you’re looking for quickly. The problem I’ve always had is that when you open a WPF file, even when you have disabled the design portion, there is still a significant time lag.

Thanks to Fabrice Marguerie who wrote this blog post which describes how to eliminate that annoying delay.

It’s nice to have less friction between me and my XAML. :)

Walking the WPF Control Hierarchy

I just finished teaching a Silverlight/WPF course last week. As is true in almost every course I teach, there were questions from people who are trying to use the technology in the real world. It's what I love about teaching...that I always learn something about how people use the tools they have been given.

In this particular case, the problem was relating to walking the control hierarchy in a user control. The user control contained a DataGrid and the goal was to get from a particular cell within the DataGrid back to the user control itself. The code that was written to do so looks approximately like the following.

public UserControl GetContainingUserControl(FrameworkElement element)


   if (element is UserControl)

      return element;


      return GetContainingUserControl(element.Parent);


The problem with the code is that, in the case specified, the Parent becomes null before the UserControl is found. For most developers, this is a strange and unexpected turn of affairs. For most hierarchies, it seems completely reasonable to presume that every element in a hierarchy will have a non-null parent except the root of the tree. But if you read over the description of the Parent property closely, you will see that it could be null. From MSDN,

Parent may be a null reference (Nothing in Visual Basic) in cases where an object was instantiated, but is not attached to an object that eventually connects to the Silverlight RootVisual, or the application object.

Ouch. Sure it's documented, but still. Ouch.

Fortunately, there is a solution, found in an incredibly useful class named VisualTreeHelper. This class exposes a number of methods that can be used to navigate up and down the visual hierarchy in a WPF interface component. There is, for example, a GetChild method that retrieves a particular child element. As well, of particular interest for this example, there is a GetParent method that retrieves the parent of a given element. This method will not return a null parent until the top of the visual hierarchy is reached.

In other words,VisualTreeHelper.GetParent(element) can be used in place of element.Parent in the above method with the result being more in line with expectations.


Summarizing the Cloud Initiative

So it's the last few hours of PDC for this year. Which means that pretty much all of the information that can be shoved into my brain has been. It also means that it's a pretty decent moment to be summarizing what I've learned.

Obviously (from its presence in the initial keynote and the number of sessions) cloud computing was the big news. This was also one of the more talked about parts of the conference, and not necessary for a good reason. Many people that I have talked to walked out of the keynote wondering exactly what Azure was. Was it web host? If so, what's the point? It's not like there aren't other companies doing the same thing. Could it be more than web hosting? If so, that wasn't made very clear from the keynote. In other words, I was not exactly chomping at the bit to try out Azure.

But it's here at the end of the week. And I've had a chance to see some additional sessions and talk to a number of people about what Azure is capable of and represents for Microsoft. That has tempered my original skepticism a little. But not completely.

In the vision of Azure was presented, the cloud was intended to be a deployment destination in the sky. Within that cloud, there was some unknown infrastructure that you did not need to be aware of. You could configure the number of servers to go up or down depending on the expected traffic to your site. As you change the configured value, your application becomes provisioned accordingly. This is nice for companies that need to deal with spikes in application usage. Or who don't have (or don't want to have) the support personnel for their infrastructure.

However, there are some limitations to the type of applications which can fit into this model. For example, you need to be able to deploy the application. This implies that you have created the application to the point where you can publish it to the Azure service. The published application might include third party components (a purchase ASP.NET control, for example), but can't use a separate third-party application (such as Community Server).

As well, you need to be able to access the application through the web. You could use a Silverlight front end to create the requests. You could use a client-based application to create the requests. But, ultimately, there is a Web request that need to be sent to the service. I fully expect that Silverlight will be the most common interface.

So if you have applications that fit into that particular model, then Azure is something you should look at. There are some 'best practices' that you need to consider as part of your development, but they are not onerous. In fact, they are really the kind of best practices that you should already be using. As well, you should remember that the version of Azure introduced this past week is really just V1.0. You have to know that Microsoft has other ideas for pushing applications to the cloud. So even if the profile doesn't fit you right now, keep your ears open. I expect that future enhancements will only work to envelope more and more people in the cloud.

WPF Futures

I just got out of the WPF Roadmap talk. I found the future for WPF less interesting that the present, although that could very well because the speed of innovation coming from that group has been very high over the past couple of years. In fact, the 'roadmap' is as much about the present as it is about the future.

Mention was made of the fact that the SP1 version of .NET 3.5 actually included a fair bit of 'new' functionality for WPF. As well, they have just released some new controls that are in different states. There is a DataGrid, Calendar control and DatePicker that are officially released. And there is a brand new ribbon control that is (I believe) in CTP mode.

As well, there is also a Visual State Manager available for WPF. This is actually quite cool, as the VSM solves a problem that WPF has (and that SIlverlight already has). Specifically, it could be challenging to get the visual state of a control/page/form matched up with the event or data triggers that would be raised within the application. The Visual State Manager lets you associate a storyboard with a particular state and then set the state programmatically, at which point WPF runs the storyboard.

Both the ribbon control and the VSM are available right now

The future for WPF seemed less clear. A list of 'potential' controls was presented. And some general comments about improvements in the rendering, integration with touch, etc. In other words, the future appears to be now. I guess that lack of specificity is to be allowed when a product group is right at the beginning of it's development cycle.

Silverlight 2.0 Penetration

I'm sure that you know that Silverlight was released a couple of weeks ago. ScottGu made an interesting comment indicating that Silverlight (that would be 1.0 or 1.1) was installed on 1 in 4 computers that are connected to the Internet. And that over the next month, those computers would be updated to 2.0. Also that they had already upgrade 100 million computers. These numbers surprised me, only in that I didn't expect that penetration had achieved that goal.

It also provides a number that might encourage people to start developing externally facing applications using Silverlight. Which, given some of the capabilities, is a good thing.

I maded you a FractLOL…

… and then I Silverlit it! Is alternately a LOLQuilt, a ROFLMosaic, or a Deep LOL! Here is the full screen version. You can click ur mouse to zoom in, shift-click to zoom out, or just use the mouse wheel to see that the image is made of thousands of LOLCats. How this came to be? The kittehs [...]

More of Silverlight sound implementation for multimedia app

Roger from asked me for more information about how I manage the sound effects in the Silverlight version of Legend of the Greasepole. I have a Canvas element in my scene called MusicCanvasRoot.  It contains a number of MediaElement children equal to the maximum number of simultaneous sounds that the game will play. This helper [...]

Back in Action

I’m back in Toronto after a much appreciated break in Italy and Greece. Before I left, I had the chance to work on a very interesting Silverlight and Windows Live project, and this week I am planning for the rest of the calendar year and eagerly anticipating the release of .NET 3.5 SP1! Silverlight Streaming was updated [...]

Another Reason to use Flash Instead of Silverlight

Flash content is finally searchable by two of the biggest online search engines on the internet, Google and Yahoo.

One of the biggest disadvantage of using Flash content for your dynamic content is simply because it is not searchable, and now with Adobe's new optimized Adobe Flash Player helping both Google and Yahoo to search and index Flash content, this means that there is no excuse left NOT TO USE FLASH on your web sites.

Note that Microsoft Live Search was not included in this partnership.

Now, for those enthusiastic about writing Silverlight content on the web, might I ask, what is Microsoft going to do about this? Both in its Live Search, making it competitive with Google and Yahoo to search Flash content, and also making Silverlight content searchable too. This is the biggest advantage Adobe Flash has over Silverlight now, and if Microsoft doesn't do anything about it, the web will not be convinced about Silverlight, regardless of the amount of bribing promoting they can and will do.

Adobe, we love you and thank you.

To those Silverlight enthusiasts, please get your facts right about Adobe Flash before even bashing Adobe Flash technology. You know who you are.