Wednesday, January 12, 2005

I was walking around CES floor and found a booth around which a long line of people was circling. At the head of the line a scruffy guy with curly hair and a short beard was signing photos. I asked someone who already got a picture, “is that supposed to be someone famous?”. The guy sputtered and said, “Of course. It’s Weird Al Yankovich

Then I went to the next booth and spotted a much shorter line with a cute chick that was signing posters, on which she modeled in some audio hardware ad. A few chinese guys were even photographing each other with her (in a “posessive” way). I decided to get a signed poster for coworker. When it was my turn, she asked who to sign it for. I asked if this was necessary. She was kind of taken aback and said that everyone wanted it signed. I agreed and gave the guy’s name. Then I asked her if that was her in the poster. She pouted and said, of course, why else would she sign them. I brought it back and it was an instant success. I became suspicious and asked “Is that supposed to be someone famous?”. The guy sputtered and said, of course, she was in those series - and then he couldn’t remember which ones. And then in a couple of hours it struck me - she was Lori Loughlin - “Becky”, “Jesse’s” wife in Full House - series that my daughter loves. Now my daughter can’t forgive me that it’s not she who got the poster.

1/12/2005 4:48:40 PM (Pacific Standard Time, UTC-08:00)  #    Comments [26]  | 
 Sunday, December 19, 2004

Q: When I append text to the end of the current content of a multiline listbox, it is always scrolled back to the first line. Even if I later use ScrollToCaret to return to the end of the text, the whole control briefly flickers. Is there a way to avoid it?

A: Indeed there seems to be an issue in Compact Framework where replacing selection in an edit control. Apparently someone wanted to be clever and even though there is a dedicated control message EM_REPLACESEL, he implemented set_SelectedText in the following way:

string SelectedText
{
   
get
   
{
       
if
( SelectionLength == 0 )
           
return
"";
       
return new string
(Text.ToCharArray(), SelectionStart, SelectionLength);
    }
   
set
   
{
       
this.Text = this.Text.Remove(SelectionStart, SelectionLength).Insert(SelectionStart, value
);
        SelectionStart =
this
.Text.Length;
        SelectionLength = 0;
    }
}

 

Why is it done this way, I don't know. There are perfectly good tools at the edit control message level to work with selection. What's important here is that set_SelectedText internally calls Control.set_Text which translates into WM_SETTEXT message. In case of an edit control it has a side effect of moving caret to the beginning and scrolling the text to the same place, thus creating an unpleasant flicker.

To remedy this problem we simply replace set_SelectedText with our own implementation:

Instead of
textBox.SelectedText = "A quick brown fox jumped over lazy dogs. ";
use
SendMessage(hWnd, EM_REPLACESEL, false, "A quick brown fox jumped over lazy dogs. ");

You will also need the following PInvoke definitions:

 

const uint EM_REPLACESEL = 0xc2;

[DllImport("coredll")]
extern static IntPtr GetCapture();
[DllImport("coredll")]
extern static int SendMessage(IntPtr hWnd, uint Msg, bool WParam, string LParam);

 

To get HWND of your textbox use something like this:

 

private IntPtr GetHWND(Control ctl)
{
    ctl.Capture =
true
;
    IntPtr hWnd = GetCapture();
    ctl.Capture =
false
;
   
return
hWnd;
}

 

12/19/2004 10:12:42 PM (Pacific Standard Time, UTC-08:00)  #    Comments [2]  | 
 Tuesday, December 07, 2004
Pioneered by Amazon (IIRC) the unobtrusive suggestions like "Other customers who bought Terminator II, have also purchased Eraser" actually make sense. That is until someone gives a whole new meaning to the word "often".
 

"Customers who bought this item often buy"... Locking Key Box (48 hooks) and ... T D INdustrial Compound Laser Miter Saw. Really???

Or this one:  ATN Night Vision spotting Scope

"Customers who bought this item often buy" Cosmopolitan Leather Tote Bag (three colors)

Well, this one actually makes sense. After all you do need something to tote your shiny new night scope around.

12/7/2004 4:43:55 PM (Pacific Standard Time, UTC-08:00)  #    Comments [0]  | 
 Friday, December 03, 2004

It just occured to me that the way we potty-train small children so that they stop making messes, we also should train older children to look the stuff up instead of asking endless questions. More and more I find myself answering another question like Daddy, what is Cassinian Oval, with “lookitup”. And what better place there is than Google (with all caveats related to the safe search issues).

I even went as far as coining a term - “google-trained”, although in my case I send her to Encarta first. The thing is that google has already become synonymous with search (same as Xerox meant “to copy”). Training by the way includes an ability to recognize a potentially unsafe or junk result.

Are your children google-trained?

12/3/2004 6:15:08 PM (Pacific Standard Time, UTC-08:00)  #    Comments [0]  | 
 Sunday, November 21, 2004

At the heart of our production application is a paradigm of a work order. As one can surmise, the real-life work order has quite a complex data structure. In our case it is a chunk of XML couple of thousands of bytes in size. When you need to edit this XML in your app, you have to keep writing long and unpleasant queries like this:

txtWONumber.Text = objWO[”WorkOrder”].GetAttribute[”Number”];
txtWOTask.Text = objWO[”WorkOrder”][”Asset”][”Task”].InnerText;

Of course in part this is caused by lack of XQuery support in CF 1.0, but still, later you have to write the update code and make sure you haven't forgotten to call it. And if you need to populate a listbox or a datagrid from a list of XML nodes, you are quite out of luck and in for some manual work.

And yet, the databinding underneath is highly automated and quite extensible, so let's see what we can do to make it understand XML data.

First of all, we will decide on what is our data unit (component). It will be an XML node. Component properties could be either attributes or subnodes, or even any valid XQuery expression - anything that returns data. A logical choice of data source is an XML node list (not an array). Such a list is easily obtained from an XML document via ChildNodes property of its document element.

I am not going to go into details of how the databinding works for there are many good sources that cover this process1. Instead I will simply say that at the root of custom databinding is an object derived from a class called PropertyDescriptor. We are going to derive our own class from it and call it XmlPropertyDescriptor.

Let's set some gorund rules. We want to be able to take an xml document like this:

xml version="1.0" encoding="utf-8" ?>
<
bookstore xmlns:bk="urn:samples">

<book genre="novel" publicationdate="1997" bk:ISBN="1-861001-57-6">

<title>Pride And Prejudicetitle>

<price>24.95price>

book>

<book genre="novel" publicationdate="1992" bk:ISBN="1-861001-45-3">

<title>The New Dawntitle>

<price>29.95price>

book>

<book genre="novel" publicationdate="1991" bk:ISBN="1-861001-57-8">

<title>Blue Smoketitle>

<price>19.95price>

book>

bookstore>

and bind to a list of “book” items using display expression like “title” or “@bk:isbn" . If we had more complex book item structure, the binding name could be a more complex path - “author/Name/@First”. What we do not want to do (since we don't really have XQuery/XPath support in CF) is to allow relative paths, functions and queries. Moreover, we will say, that in the path, every element must be a node name, except of the last one, that can be an attribute name (starting with @ ).

When you derive from the PropertyDescriptor class there are few things to keep in mind:

  1. Override IsReadonly property. This will tell the framework whether your property supports updates
  2. Override PropertyType property to return typeof(string) - this is our property type; we don't do any data conversions here. If we want data conversion/validation, we can implement it in Format/Parse events of the binding. We could to some data validation and type conversion via reading the schema (if available), but it would be outside of the scope of this article.
  3. Override ComponentType property to return typeof(XmlNode). This is the only component type we deal with.
  4. Override GetValue method. This is the most important method. It actually goes ahead and retrieves the property value given the component. In our case it gets the XmlNode (and already has the “XPath” by way of constructor parameter). Our task is to perform the query. Very simple on the desktop. A bit more complex in CF.
  5. Override SetValue method. It receives the component and the new value and sets it using the known “Xpath”.

Now, wouldn't it be nice to make the binding use our XmlPropertyDescriptor class? Simple - we just need to create our own Data Source that would report XmlPropertyDescriptors instead of whatever is used by CurrencyManager. Whatever is usually a SimplePropertyDescriptor class, ReflectPropertyDescriptor or DataColumnPropertyDescriptor.

This brings us to building our own data source object. There is no abstract class to override. When creating it, we need to make sure we implement several interfaces:

  • ITypedList
  • IEnumerable
  • IList
  • IBindingList

ITypedList::GetItemProperties creates a bit of a problem. It is supposed to return Property descriptor collection populated with all properties the data unit exposes. Unfortunately in our case properties are valid xpath expressions, which could be quite a few, expecially on a complex XML document

The reason we need to implement ITypedList::GetItemProperties is that we need for example a data grid to be able to get populated automatically, without us specifying XPath expression for each column. The problem here is that we don't want to build a full list of all possible xpath expressions valid on our XML item, so we need to decide on some way to simplify it. Let's limit the autogenerated property list to all attributes or all 1-st level child elements. If the XML is more complex than that, well, you need to specify the grid columns explicitly.

The IEnumerable and IList are simply delegated to the underlying XmlNodeList. IBindingList implementation is somewhat simplistic as we don't really want to figure out how to track the external changes in the XmlNodeList.

We end up with a class that allows us to take the XML document above and write things like:


doc.Load("data.xml");

XmlDataSource src = new XmlDataSource(doc.DocumentElement.ChildNodes, XmlDataSourceMode.DataSourceModeAuto);

txtGenre.DataBindings.Add("Text", src, "@genre");

txtISBN.DataBindings.Add("Text", src, "@bk:ISBN");

txtPrice.DataBindings.Add("Text", src, "price");

txtPubDate.DataBindings.Add("Text", src, "@publicationdate");

txtTitle.DataBindings.Add("Text", src, "title");

 

listBox1.DisplayMember = "title";

listBox1.ValueMember = "@bk:ISBN";

listBox1.DataSource = src;

dataGrid1.DataSource = new XmlDataSource(doc.DocumentElement.ChildNodes,

new string[] { "@genre", "title/#text", "price/#text" });

This produces a screen like this one:

 

Selecting a list box item moves the current position along the node list. Data edits are also supported.

The source code for this article can be found here.

11/21/2004 8:41:11 PM (Pacific Standard Time, UTC-08:00)  #    Comments [0]  | 
 Sunday, November 07, 2004

I've posted an article discussing the event handler internals, tips and tricks

11/7/2004 1:19:43 AM (Pacific Daylight Time, UTC-08:00)  #    Comments [0]  | 

After working on my first speech server project for a week or so, I decided to test it on the real hardware (as in connected to the analog phone line). I already had a D/41JCT-LS board from Dialogic (er, Intel). The 180-day evaluation of the Speech Server is available from Microsoft web site, and Intel kindly offers a 60-day evaluation of their NetMerge TIM. Sounds simple. But having spent some time developing for CT Media product in the past, as well as some other telephony products (both enterprise and consumer), I knew I was bound to encounter some resistance.

Conveniently, both Speech Server and the NetMerge thing come without any sort of installation guide. Speech Server mandates 2.5+ GHz machine with 2GB RAM. I decided that a newly acquired Sempron 2400+ motherboard with 512 MB should therefore fit the bill.

The first roadblock was in the form of Speech Server refucing to install caliming it has expired. Moving the clock back to 1/1/2004 helped. Then I installed NetMerge (I will omit the gruesome description of the download process - Intel site kept resetting connection, so you would do well by getting a download manager of some sort). NetMerge installed without any complaints. NetMerge package also includes the driver for the PCI board.

At this point a lesser mind would have been tempted to make a call to the new server and see whether it works, but I calmly decided to take it easy. The NetMerge program group contained two demos. I launched the first one only to see mutiple complaints about not being able to acquire speech resources. Indeed, when I tried to call in, the system responded, but would ignore me speaking into the handset. To fix this I went into DCM (Dialogic Configuration Manager) and (after stoppping the service) in the card properties made the following changes:

  • Changed the PCM Format from Automatic to mLaw
  • Instead of the defaullt firmware, selected the one from the dropdown (d41jctsp.fwl)

After restarting the service, I ran the demo again and lo and behold - the bargein worked, the demo was fully operational.

Encouraged I tried starting the Speech Server (TAS) only to find out that it still wouldn't start. The event log was complaining of the lack of voice resources. This was strange, since I was pretty sure that the recent configurastion change in the board properties should have taken care of that. Unfortunately the TAS still refused to start. It took me some time to finally find an application in the Speech Server program group called “Configure Resources”. Upon being launched it opened DOS window and happily reported having found 4 voice resources. This was it. The TAS started successfully and in a minute I was able to call in and hear “Welcome to Microsoft Speech Server”

11/7/2004 1:17:37 AM (Pacific Daylight Time, UTC-08:00)  #    Comments [0]  | 
 Thursday, October 21, 2004

I've posted an article that discusses how to create non-rectangular windows in Windows CE using Compact Framework. See it here

10/21/2004 10:27:51 AM (Pacific Daylight Time, UTC-07:00)  #    Comments [0]  | 
 Sunday, October 03, 2004

When populating a listview it is always a good idea to call BeginUpdate before adding/removing/clearing items. Otherwise the control will attempt to repaint on every change, greatly reducing the overall performance.

A question often asked is what to do with Listbox and Combobox classes, which in Compact Framework do not expose these methods. Apparently it is quite easy to emulate missing methods.

The secret is in WM_SETREDRAW message. It tells the window to stop updating itself. The actual implementation is up to the window class, but luckily in case of Combobox and Listbox this message is handled and provides expected results. Here is what is says in remarks part of the message description:

 The window manager provides no default processing for this message. However, certain controls do support this message, list box, tree view, and combo box

Armed with this knowledge, we find it almost trivial to write the missing methods:

public class UIHelper
{

 public static IntPtr GetHandle(Control ctl)
 {
  ctl.Capture =
true;
  IntPtr ret = Win32Window.GetCapture();
  ctl.Capture =
false;
  return ret;
 }

 public static void EnableDisableUpdates(Control ctl, bool bDisable)
 {
  if ( ! (ctl is ListBox) && ! (ctl is ComboBox) )
   throw new NotSupportedException("Control must be either listbox or combobox");

  IntPtr hWnd = GetHandle(ctl);
  Win32Window.SendMessage(hWnd, WM_SETREDRAW, bDisable? 0: 1, 0);
 }

 const int WM_SETREDRAW = 0x0b;
}

Notice

This code uses Win32Window class found in OpenNETCF SDF. If you don't want to use SDF, the P/Invoke defionitions for GetCapture and SendMessage can be found here.
10/3/2004 6:28:01 PM (Pacific Daylight Time, UTC-07:00)  #    Comments [0]  |