Creative Gift Carding …

My parents, on Christmas Eve wanted to surprise everyone with a gift card to a store. But for fun, they wanted it to be a bit random. So, they taped a gift card to the bottom of all of the plates (in a buffet style dinner), so that when someone grabbed a plate, they discovered a gift card! They didn’t mention they were doing this – so at first we didn’t get it – but then … hey! Gift Cards! Cool!

Do you worry about the URL?

Guess what content this URL points to…

image

Hard to guess, isn’t it?

If you guessed the WEATHER, you’d be right.

I had typed in the domain name followed by /weather, thinking that would take me immediately to the weather page, but was greeted by a 404 Error Page (not a nice one … just the one built into the browser).

When you’re coding a web application, do you make it so well known/used destinations are easy to reach by using clean/simple URLs?

Do you like it when a web application uses URLs that are understandable, rather than everything being codified?

Silverlight’s ItemsPanelTemplate, Horizontal Only?

Have you tried to create a horizontally oriented ListBox in Silverlight with a custom ItemsPaneltemplate, with a Panel of type other than StackPanel? If you have, you’ll likely have run into the same problem I have – you can’t really do it. Unfortunately, the ItemsControl (which the ListBox derives from), eventually checks the Panel type of the ItemsPanel (the ItemsHost), and it only reacts properly to a Vertical orientation if the Panel type is a StackPanel.

internal bool IsVerticalOrientation()
{
    StackPanel itemsHost = base.ItemsHost as StackPanel;
    if (itemsHost != null)
    {
        return (itemsHost.Orientation == Orientation.Vertical);
    }
    return true;
}

You might try deriving from StackPanel, but unfortunately, ArrangeOverride and MeasureOverride are both sealed methods on the StackPanel. So, you can’t. This behavior certainly limits what you can do with a ListBox layout.

What happens if you try something vertically oriented without a StackPanel host? The behavior is that the ListBox won’t properly scroll and activate the current ListBoxItem. Calling ScrollIntoView has no impact (as the math doesn’t work correctly when the IsVerticalOrientation isn’t properly handled).

Yet another feature of WPF that you’ll need to ignore for the time being. 🙂

Converting RESTful XML output to formatted XML in Silverlight

Ever debugged output from a RESTful web service and was driven absolutely mad by the lack of formatting of the XML? Seriously, how much fun is it to stare at XML that has no line breaks or indentation whatsoever?

I haven’t tried to solve the problem within the IDE as that’s a different problem. However, I have written the code to convert the output into something that you could stuff into a TextBox for example (which is what I’ve done in my Silverlight application – I’ve created a small debug window for RESTFul web service calls).

 

HttpWebRequest request = (HttpWebRequest)
    HttpWebRequest.Create(new Uri("http://localhost:12345/MyWebService.asmx"));

request.BeginGetResponse(delegate(IAsyncResult ar)
{
    HttpWebRequest req = (HttpWebRequest)ar.AsyncState;
    HttpWebResponse webResponse = req.EndGetResponse(ar) as HttpWebResponse;
    try
    {
        using (Stream stream = webResponse.GetResponseStream())
        {
            XmlReader xmlReader = XmlReader.Create(stream);
            try
            {
                XmlWriterSettings settings = new XmlWriterSettings();                            
                settings.Indent = true;
                settings.Encoding = System.Text.Encoding.Unicode;
                using (StringWriter strWriter = new StringWriter())
                {
                    XmlWriter xmlWriter = XmlWriter.Create(strWriter, settings);
                    try
                    {
                        // cool, this recursively writes all of the nodes!
                        xmlWriter.WriteNode(xmlReader, false);

                        // output it to the UI as a string 
                        Dispatcher.BeginInvoke(delegate()
                        {
                            txtRawResults.Text = strWriter.ToString();
                        });
                    }
                    finally
                    {
                        xmlWriter.Close();
                    }
                }
            }
            finally
            {
                xmlReader.Close();
            }

            stream.Close();
        }
    }
    finally
    {
        webResponse.Close();
    }
}, request);

Replace the Uri in the first line with a call to your web service that returns XML.

Steps:

  1. The Response is requested asynchronously (as is the only option with Silverlight)
  2. Within the anonymous method, the request is retrieved from the IAyncResult object instance (ar)
  3. The async request is properly ended (EndGetResponse) and retreieved
  4. The stream is fetched from the response and passed to a new XmlReader instance.
  5. Create a new instance of the XmlWriterSettings object with indentation activated (Indent = True)
  6. Create a new XmlWriter instance using a new instance of a string writer and the settings created in step #5
  7. Recursively (thanks to the WriteNode method), write the entire document to the writer, formatted with indentations as requested!
  8. Invoke back to the UI thread with the result of the formatted, recursive WriteNode call.
  9. Clean up.