I was working on a Silverlight application with a WCF RIA Services middle-tier recently when the need arose for us to add an HTML/Javascript front-end in addition to the existing Silverlight one. I knew Microsoft offerd WebAPI services that are specially geared toward being consumed by Javascript clients, but the team wanted to leverage the existing work we'd done on the middle-tier in RIA Services if possible.

So the question was, could I publish my existing RIA domain service in a way that is Javascript-friendly? WCF RIA Services exposes its domain services in a binary-format by default. Javascript works better with a ...

The Problem

I recently discovered that the default Silverlight DateTime format is different on MacOS than it is in Windows.

 

On Windows

windows

 

On MacOS

mac

 

There are a couple of differences, but the most noticeable is that time zone offset at the end of the time that shows on the Mac. On my computer the time zone offset shows as “-4:00” because I’m generating a DateTime in local time, which for me is currently Eastern Daylight Time (EDT), or 4 hours behind Coordinated Universal Time (UTC).

 

The application is following the best practice of storing all DateTimes ...


What I'm Up To

This week, I'm learning iOS development after 13 years of programming on the Microsoft stack (primarily .NET). Developing for iOS requires a computer running MacOS, which in turn requires a Mac (ignoring the rather iffy option of running a Hackintosh).

I first learned to program on two Apple computers--BASIC on an Apple IIe and C++ on a Mac LC. It's been many years since those machines were on my desk, and my regular Mac use has been a bit spotty since then. As a result, I'm having to refamiliarize myself with some of the little differences ...

I was working with a client recently when I encountered a completely unexpected bug. Figuring out the problem reminded me of some facts about how C# (and all the .NET languages) work and got me thinking about the fact that object-type variables in .NET are basically pointers, even though it’s hidden from you most of the time.

The Problem

An object instance was passed into a method. If the parameter was not null, it was simply updated, but if the parameter was null, then it was instantiated first, then updated.

It looked something like this:

class Widget
{
    public...

I’m a huge fan of the Microsoft Prism framework. In fact, I’ve blogged before about using its DelegateCommand to minimize your ViewModel code.

Lately, though, I noticed that once its CanExecute delegate returns false, your IsEnabled binding will not update, even when the property it’s bound to changes.


Try It Out: Live Example

Note: Silverlight 4+ Plug-in Required
Get Microsoft Silverlight

 

As you can see, the button entitled “Prism Command” starts out disabled and never enables, even though its IsEnabled property is bound to the same property of the ViewModel as the other buttons (see below).

 

<Button Content=...

I was recently asked by a client to help upgrade their application from Silverlight 4 to Silverlight 5. The upgrade worked perfectly except for one third-party assembly that had not yet been upgraded to Silverlight 5. The client only needed to use this assembly to generate QR Codes--those black-and-white barcode squares you often see people scanning on their smartphones—so my task was to find them a replacement QR Code library.


Sample QR Code

My colleague, Josh Eastburn, pointed me to a free option that was created for use in WP7 apps, but works just fine in Silverlight 5. You ...

If you’ve been programming in Silverlight for long, you may have noticed the project setting “Reduce XAP size by using application library caching” in you Silverlight project’s settings.  You may have even checked it on to see what happens.

If you did try it, most likely nothing happened.  This is because there are a few simple prerequisite steps you have to take in order to take advantage of application library caching. 

In this blog post, I’ll step you through those set up steps.  But first, why would you want to use application library caching?

 

Why Use Application Library Caching?...

My friend Basem has a nice blog post about the most useful features of the Productivity Power Tools extension for Visual Studio 2010.  It’s definitely worth a read if you haven’t read it already.  Like Basem, I love the Productivity Power Tools extension and use most of it’s features on a daily basis.  And, until recently, I’d run into only a very few issues with it, all of which I’d classify as minor annoyances. 

Recently though, I ran into one issue that put me off track for a while trying to figure it out.  It happened when I created a ...

Have you ever created a UI element in Silverlight (a Grid, Rectangle, Border, etc.) with transparent components and wondered why it was so hard to click on?  Most likely, it’s because the background or fill brush of your element is null.  Null fill and background brushes allow mouse events (and touch events for WP7 apps) to go through to whatever is behind the element in question.

Instead, you can set a fill or background brush to the pre-defined color “transparent”, and your element will now always intercept mouse and touch actions and raise the corresponding events.

Below is a simple ...

Someone recently asked me for advice on how to bind the visual state of a Silverlight control to a property of the view-model.  Specifically, they had a Boolean property on their view-model named something like IsExpanded that they wanted to control the expanding and collapsing of a control in the view with the value of this view-model property.

It was such a fun sounding challenge—and one that seemed like it would be very useful to have for many situations—that I couldn’t resist creating a simple solution. 

Here’s What I Created (Silverlight 4 Plug-In Required)

Get Microsoft Silverlight

 

Here’s How I Did It...

Snickers fans know how nice nougat can be, even if they don’t really know what it is.  Microsoft produces a different kind of NuGet that’s also really nice and not well understood.

The NuGet site on codeplex says that it is a “package management system for the .NET platform intent on simplifying the process of incorporating third party libraries into a .NET application during development”. 

That’s a good summary, but here’s how I think of it--I no longer have to worry so much about keeping all my .NET tools and libraries up-to-date or spend a lot of time figuring out ...

Introduction

As most .NET programmers no doubt already know, casting is required when converting from one type to another type when information might be lost, also now as narrowing conversions.  In C# there are two different ways to cast:

1) Casting using the prefix style

object value1 = 123;
string value2 = (string)value1;

 

2) Casting using the “as” keyword

object value1 = 123;
string value2 = (value1 as string);

There are some simple and arbitrary reasons to use one of these over the other.  Perhaps the most arbitrary reason people use one method or the other ...

I love the M-V-VM pattern for Silverlight and WPF applications.  It’s beautiful and works very well, but sometimes it is too verbose, particularly in the ViewModels’ INotifyPropertyChanged implementations.  I find that I spend too much time on the repetitive plumbing tasks that could otherwise be spent programming the actual application logic.

There are many tools and frameworks out there to speed up and simplify MVVM (Caliburn, MVVM Light, WAF, etc.), but I just discovered a very simple tool that goes a long way toward making MVVM more useable. 

It’s called NotifyPropertyWeaver and it solves the problem ...

Even though I’ve been doing ASP.NET for 8 years, I never realized until today that when you put a breakpoint in Application_Start in Global.asax, it may never be hit.  The reason, I discovered, is that the debugger doesn’t have time to attach to the web application before Application_Start finishes.  To get around this, you can force your application to restart after the debugger has had a chance to attach by simply editing and saving a change the web.config.  This causes the application to restart and your breakpoints in Application_Start will now be hit.

There has been some angst in the .NET development community lately as Microsoft has started putting a lot of emphasis on HTML5, especially leading up to the release of IE9.  The issues center around the overlap in capabilities provided by both technologies and Microsoft’s seemingly incongruous dedication to both.

Well, the Silverlight Team is now weighing in on the controversy on their blog here: http://team.silverlight.net/announcement/standards-based-web-plug-ins-and-silverlight/

To summarize, they are saying that software development has always required an array of tooling choices--some with some feature overlap--but that Microsoft sees different advantages and disadvantages to both HTML5 and Silverlight, and that leads ...

Fans of Internet standards, rejoice!  Today Microsoft officially brings us Internet Explorer 9 which scores a 95 out of 100 on the ACID3 test and passes the CSS Selectors Test with flying colors.  It’s worth noting that IE9’s ACID3 score is one point more than the current version of Mozilla’s Firefox, but still five points below Chrome and Safari.  Many developers will also be very happy to know that this latest offering from Microsoft implements many of the new features of HTML5!

To me, IE9 is a story of Microsoft finally starting to catch up to the other browsers, but ...

UPDATE May 4, 2011: It was brought to my attention that there was a typo in my code below, but it is now fixed.

I sometimes find it necessary to walk the visual tree of controls in Silverlight (or WPF) at run-time.  The VisualTreeHelper class provides some nice functionality for doing this.  However, using the VisualTreeHelper can sometimes require a little too much code, especially when I want to search the visual tree recursively. 

For this reason I created some simple extension methods to do the recursing for me.  Let’s cut right to the chase.  Here’s the code:

Few things are more disappointing to an ASP.NET programmer than this one line of code in a web.config:

<sessionState mode="Off" />

You probably know what I mean if you’ve ever been asked to implement user sessions for a web site that has ASP.NET’s built-in session state disabled.  In this post I will offer one simple alternative that allows you to implement user sessions on just such a web site.

Cookies Taste Better In-Memory

The secret to my solution is HTTP cookies.  In my experience, programmers are increasingly afraid to create sites that require cookies because ...

There are many ways to extend classes in .NET languages, but I thought I’d cover just a few of the more common or compelling options here.

Derivation/Inheritance

Perhaps the most common type of extensibility, certainly the most obvious, is creating a class that derives from a base class to add new functionality.  This is called inheritance and has been a part of .NET (and all other OO languages) since the beginning.

class Base
{
    public bool IsBase;

    public Base()
    {
        IsBase = true;
    }
}
          
class Derived : Base
{
    public bool IsDerived;

    public...

I was recently asked to create an ASP.NET 2.0 web site that consumed an OData (WCF Data Services) data feed.  Of course, in .NET 4.0, this would have been easy given the great tool that Microsoft has provided for us in the WCF Data Services Client (System.Data.Services.Client).  However, in .NET 2.0, this presented somewhat of a challenge.

For all the examples in this blog, I’m using Chris Woodruff’s baseballs stats feed located at http://baseball-stats.info/OData/baseballstats.svc/

I decided to write my own simple OData ATOM/XML parser.  But first, I wanted some classes to hold.  Ideally, I wanted classes that ...