Windows 8.1 Masked TextBox Behavior

Shortcut to the MaskedTextBoxBehavior on GitHub.

Lack of control

One of my (and several others’) main complaints about the Windows Store App dialect of XAML is the lack of many features/controls that we found in WPF and even Silverlight’s dialect.  Windows 8.1 XAML added some back in (like a Date Picker) but one major lacking control is a Masked Text Box where the user’s input could be guided through a mask (like entering in a phone number).  While WPF lacked one, there were plenty of example out there of people that built them (yaay open source) as well as third party options.  Yet Windows Store Apps had none with the exception of a couple commercial products (DevExpress, for example).  I personally felt there should be one out there in the open source world … so I made one (yaay open source).

Control behaviors

One cool thing I like about XAML controls is the ability to extend the behavior of the control without sub-classing through the use of behaviors.  This allows the developer to use whatever object-oriented hierarchy they want and still pile on different/additional functionality of the control.  This was the approach I choose to take for (my attempt to) add a masked input behavior to Windows Store XAML’s TextBox.

More lack of control

I mistakenly thought this would be a relatively straight-forward task but Windows 8.1 once-again removed some useful functionality like the Preview events, etc. I was left with three events: KeyUp, KeyDown, and TextChanged.  It would have been real nice to use the KeyDown/KeyUp events but I was only provided a “VirtualKey” which was going to make things even more cumbersome so I had no choice but to use TextChanged.  Problem is, TextChanged gives me the text after the user has already entered it.  Time to get creative …

Regular Expressions, Oh My!

Instead of writing my own pattern matching / string parsing logic I decided to rely on Regex processing within the .NET Framework.  However, I didn’t want users of this control to have any needful knowledge of the inner logic so I created a straight-forward expression syntax (like ###-##-####) and generated a regular expression syntax from it.  At this point, it was just a matter of handling most of the fringe cases, copy/paste, etc.

Go and get it! Critique it! Fix it!

In the spirit of righting the wrong I tossed my simple version of this behavior up on GitHub with usage instructions, etc.  It is likely there are better ways of handling things to I welcome any and all contributions and critiques on this!

Leave a comment here if you find it useful!

Steel City Codefest 2014 – The Checkpoint

Finally getting around to posting about my experience at this year’s Steel City Codefest where I worked with a team of my colleagues to whip up a prototype of an app for a non-profit organization:


This particular project was to create a mobile app (cross-platform) for veterans to locate and review/rate resources and services. While this doesn’t sound like a particularly flashy application to work on, we felt it was one with more need than the others (plenty of teams volunteering for other projects) and the challenge was really to try to make something useful and interesting.


Initially I had no plans to participate in Codefest but a few of my colleagues at work were joining in so I figured I would give it a shot.  We all have many years of experience working on a variety of technologies but we all shared a common background for C#/.NET so we decided to leverage Xamarin’s cross-platform native app framework so that we could all contribute as equally as possible.  Since we were targeting both iPhone and Android we felt that we were too limited by a hybrid-mobile solution and didn’t have a big enough team to go full native (Objective-C and Java) so Xamarin fit right in the middle.

Additionally, we wanted to share as much code as possible (really the only way to recoup the cost of tools/platforms like Xamarin) so we decided to incorporate MvvmCross, a framework for Xamarin allowing us to only need to re-write the view layer and share all the way down through the ViewModels and so on.


While we had done some initial planning after work, we pretty much came in with a blank slate ready to get started.  As I was the only one with a MacBook, I was on the hook for ripping out the iPhone version, Geoff had some Android experience so he was tackling that, Mark started working through integration with FourSquare, and since our designer was a no-show, Steve got stuck with the UX/UI design work and also kicking in on the ViewModels.

15 straight hours of coding later and out pops a prototype of an app:

20140224_142409000_iOS 20140224_142417000_iOS 20140224_142421000_iOS


I am planning on finishing this app as soon as I get a chance to get working on it.  To help, I am working with Jared from Checkpoint to apply for some grant money to cover the cost of licenses (Xamarin), services (some mobile backend-as-a-service) and some time/help/kindness of others!

MacBook Pro 13 Retina + Windows 8.1 Boot Issue

Just unboxed a new MBP 13″ Retina and started the Bootcamp process to dual boot with Windows.  Once I finally got to installing Win 8.1, I rebooted to complete the setup and all I got was a black screen with a blinking cursor.  Searching around I saw lots of options for setting up a repair disk, etc. but being a fresh install I just wanted it to work “out-of-the-box.”  So I continued to investigate…

I suspected the issue had something to do with the hybrid GPT/MBR partition that Bootcamp creates so what I did was re-launch the Windows 8.1 setup (from USB) and deleted the (OS X created) BOOTCAMP partition and let the Windows installer create it.  Now works like a champ.


My beach experiment with the Kindle Paperwhite

I’m connected, lead a connected life, and I can’t live without being connected. So, every year when traveling to the beach I am faced with the challenge of trying to read my iPhone’s display in the stark sunlight. This year I decided to try a little experiment: “Could I be with an e-reader with an e-ink display?”

First, I needed to find a device with a usable browser, understanding that it would not be a first-class experience and that I would depend on tethering to my iPhone and that the refresh rate of an e-ink display might limit what I could do.

Choosing – Winner: Kindle Paperwhite

The day before we left for the beach (I only buy electronics on impulse), I headed over to the local electronics retailer to compare the Nook to the Kindle.  The Nook was what I assumed I was going to pick up: allegedly faster refresh, micro SD slot, more format support and so on.  However, it is difficult not to admire the tech behind the Paperwhite’s screen and, in my opinion, I found the it a little nicer in the Kindle.  The second factor was that most of the Nooks I see on display are busted.  Either they are so popular that they get handled a lot or they are fragile.  I went with the Kindle.  While I would miss the micro SD slot I have come to like just emailing my Kindle anything I want to sync to it.  I could just use the USB cable but I hate tethering.

Getting Content – Feedly+Instapaper = WIN!!

My primary beach activities (other than burying my daughter in sand) include reading my way-old stack of magazines and reading my (RSS) news feeds.  Funny enough, many of my magazines have gone totally digital or I canceled as I just never have time to read them.  What was left was a “stack” of MSDN PDFs and a new subscription to MIT Technology Review (Kindle edition).  Long story short: PDFs stink on the Kindle (you can zoom in/out but turning the page os just too slow) but the Kindle edition of the magazines are really nice.  As a note, I can convert the PDFs into .mobi by emailing them to Amazon and they’ll just appear on my device.  MSDN mag did not survive the conversion process :(

Since the demise of Google Reader, I moved on to Feedly for my RSS feed madness.  Generally, I scan through headlines on my iPhone or laptop and read just a few articles but Feedly would simply not work in the Kindle’s browser. Then I re-discovered Instapaper. Feedly integrates with Instapaper where, when not on the Kindle, I can simply flag articles I want to read later.  Then, with a single push of a button, I can have those web articles delivered to my Kindle in e-reader (.MOBI) format.  Now they are super easy to read and waiting for me.  Brilliant!

Staying Connected – Kindle’s Experimental Browser + Gmail + Twitter

Once I inevitably get bored of reading, I move on to checking email and tweets.  The good news is that Gmail and Twitter are perfectly usable in the Kindle’s browser.  That’s not to say the experience is awesome … but it easily gets the job done and I don’t have to struggle to see what I am reading in the bright sunlight.  My corporate webmail client, however, is totally unusable … what a shame :D

Most other web sites work to some degree in the browser as long as they aren’t getting crazy with the rich media.

Conclusion – Success!

After using the Kindle this way for a few days, I am very pleased with the experience. In fact, I wrote the intro to this post with the Kindle itself.  The spelling mistakes (from the slow refresh and my fat thumbs) were a little comical and required some post edits but it was fun anyway.

.NET Reactive Extension – Pittsburgh Tech Fest

What a great Pittsburgh Tech Fest this year!  Lots of great sessions, attendance was awesome and I met lots of like minded folks!

Thanks to everyone that attended my talk on .NET Reactive Extensions! Hopefully everyone was able to walk away with some useful information and maybe some ideas to try!

You can get the slides and code (as presented at Tech Fest) from my GitHub repo here:

Thread.Sleep (asynchronously) in a Windows Store App (WinRT)

UPDATE:  Another option is to use await Task.Delay(ms) which would require you to be in an async method (which my examples are).

I thought I had a simple idea that would require simple code: In my “service mock” class I wanted to sleep the thread for 2 seconds to simulate a real web service call to see how my UI reacted.

Simple, right? Just throw a good-ol’ Thread.Sleep() in there and all is well? Nope!

Issue #1 – System.Threading.Thread is no more …
Luckily, a quick Internet search (I refuse to turn the name of my search engine into a verb) revealed a “hack” to sleep on a thread:

new System.Threading.ManualResetEvent(false).WaitOne(ms);

Issue#2 – But it blocks my thread.
Ok … but this blocks my thread and C# 5 gives me a nice async/await pattern so as not to block my thread and MRE is not awaitable.  Ok … I’ll just spawn off a task and sleep on it.

await Task.Run(() => { new System.Threading.ManualResetEvent(false).WaitOne(2000); });

Or use Task.Delay inside an async lamba or method:

await Task.Delay(2000);

There you have it.  I can now simulate a slow web service call in my stub/mock service implemenation.

Post a comment if there is a better way to do it or just to contemplate the deep meaning of  “Sleep asynchronously.”

Meta-Meta-Programming … and I like it

I have a client that needs to rapidly create websites that are similar in functionality but needs to make changes to the data model (sometimes significant changes) and have the UI get updated with it, turning around a change within the day.

For this, I decided to base a solution around ASP.NET Dynamic Data for two main reasons: 1. It’s scaffolding concept and 2. The “generated code” has been tested already, shortening the testing cycle as rapid changes are released.

Of course, the out of the box user experience with Dynamic Data leaves much to be desired and is a little too simple for our needs so it requires some customization.  Once the customizations are done, they can be re-used over and over again but ONLY if they are written against a meta model so that they can apply to any entity on our model, not just coded against known entities, etc.

So what’s “Programming”?

In the normal case, programming is simply putting down the instructions required to make our applications work.  This still holds true in Dynamic Data and other “frameworks” … we are just walled into a set of constraints and conventions.

So what’s “Meta-Programming”?

In Dynamic Data, when creating customizations we have to interrogate the meta-data about the model and code against that instead of directly against the known model. We already have the Type object in .NET to discover much of what we need about the model (data type, field names, etc.) and we can enrich this model with attributes for information like descriptive names, validation rules and so forth.

This makes typical “programming” tasks more challenging starting with simple tasks like presenting the data on the screen to querying the data source without actually knowing the model ahead of time.  LINQ queries change from a simple query syntax to expression trees.  Once you get in the right mindset … it’s kinda of fun.

So what’s “Meta-Meta-Programming”?

This is where the real fun starts.  In Dynamic Data, we are using Model-First Entity Framework to essentially drive the entire application.  This generates the model, which we mark up with the metadata so we can “meta-program” around it.  But what if we can also generate the metadata?

By default, the Entity Object code generator creates decent classes which match the model, play nice in OOP and who’s persistence is managed though the generated ObjectContext. But there are lots of fields on the Entity Model which are largely untouched. What if we could “program” the entire application through the Model and with maybe a little side of metadata on the attributes?

To begin to accomplish this, we ditched the default code generation and downloaded (Nuget rocks) the EF5 Entity Object T4 template.  We then began to generate our metadata through addition fields on the model (DefaultValue, Is Nullable, Documentation).  So the T4 template with these additional fields become our meta-meta-programming tasks, we meta-program the UI to react to the metadata, and we toss in some good old fashioned programming when we get sick of expression trees and reflections.

I may write about the T4 template we created but just contact me if you want a copy in the meantime.