TabletPC Development Gotchas Part3: Coerce to Factoid (XP vs. Vista)

If you are writing (or maintaining) and application that uses handwriting recognition, please be aware of a subtle behavior difference between XP and Vista when coercing the recognition result to a Factoid (or an input scope or a word list). This may easily be missed in your application testing, but it will potentially result in unhandled exceptions from your application when consumers are starting to use it.

Here is the scoop: normally, when you assign strokes to a RecognizerContext for recognition, it will always be able to return some result - even for random scribbling. However, if you set the 'Coerce' flag to bias the recognizer towards a certain format (e.g. Date, URL, etc.) or word list (e.g. medical terms) then there is a (small) chance that it will not be able to compute a result. On Windows XP, the RecognizerContext returns a null RecognitionResult in this case. On Windows Vista, however, an InvalidOperationException is thrown.

So if you have written an application agains the XP behavior, please update your code to also catch InvalidOperationException if you expect your customers to use your app on Windows Vista. If you are writing a new app today on Vista, please be sure to also check the RecognitionResult for null if you plan to support your product on Windows XP Tablet PCs.

To clarify the issue in code, consider the following example: the goal here is to recognize a given strokes collection as URL. Now if the strokes are random enough, the recognizer won't be able to coerce it to a valid URL (with my handwriting, for example, writing "ooooo" does not yield a result).

private string Recognize(Strokes strokesToRecognize)

{

    RecognitionStatus status;

    RecognizerContext recoContext = new RecognizerContext();

    recoContext.Factoid = "(!IS_URL)";

    recoContext.RecognitionFlags = RecognitionModes.Coerce;

    recoContext.Strokes = strokesToRecognize;

    try

    {

        RecognitionResult result = recoContext.Recognize(out status);

        if (result != null)

        {

            return result.TopString;

        }

        else

        {

            // Windows XP code path

            return "Null means NO RESULT on XP";

        }

    }

    catch (InvalidOperationException)

    {

        // Windows Vista code path

        return "InvalidOperationException means NO RESULT on Vista";

    }

}

So the bottom line is: if you are writing an application that uses handwriting recognition, you should harden your code to handle both a 'null' result as well as an 'InvalidOperationException' from calling RecognizerContext.Recognize().

Next post in this series: TabletPC Development Gotchas Part4: Semantics of GetSystemMetrics(SM_TABLETPC)

Previous post in this series: TabletPC Development Gotchas Part2: Deploying InkPicture applications to Windows XP desktops