Loose Lips...

LooseLips

Tags:

Categories: catfood

XamlParseException and 256x256 icons

When testing out a WPF app on XP I got an unhelpful XamlParseException error report. 

I was a little puzzled because I was hooking up error reporting in App.xaml.cs:

App.Current.DispatcherUnhandledException += 
    new DispatcherUnhandledExceptionEventHandler(Current_DispatcherUnhandledException);
AppDomain.CurrentDomain.UnhandledException += 
    new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

My error handler was attempting to create a XAML window to report the error, and evidently this was bombing out as well triggering the good doctor Watson. I added a MessageBox call instead and discovered that the XamlParseException was wrapping a FileFormatException and the stack trace indicated that the problem was with setting the icon for the window. After removing the icon the app started up fine. Weird.

It turns out that WPF chokes on a compressed 256x256 icon on XP and Vista (Windows 7 seems to cope fine). Saving the icon without compression fixes the problem. I use IcoFX and you can set this at Options -> Preferences -> Options -> Compress 256x256 images for Windows Vista. Of course the consequence is that the icon is a couple of hundred kilobytes larger.

Tags: , ,

Categories: .NET | C#

Space and multibyte character encoding for posting to Twitter using OAuth

I've spent the last day learning how to use OAuth and XAuth to post to Twitter. There are rumblings that Twitter will start to phase out basic authentication later this year, and more importantly you can only get the nice “via...” attribution if you use OAuth (for new apps, old ones are grandfathered in).

I coded up my own OAuth implementation, referring to Twitter Development: The OAuth Specification on Wrox and the OAuthBase.cs class from the oauth project on Google Code. Both are great references, but both fail with multibyte characters. The problem is that each byte needs to be separately escaped. OAuthBase.cs encodes characters as ints rather than breaking out the bytes and the Wrox article incorrectly suggests using Uri.EscapeDataString(). 

Here's a method to correctly encode parameters for OAuth:

public static string OAuthUrlEncode(string s)
{
    if (string.IsNullOrEmpty(s))
    {
        return string.Empty;
    }
    else
    {
        StringBuilder sb = new StringBuilder(s.Length);

        for (int i = 0; i < s.Length; i++)
        {
            if (NoEncodeChars.IndexOf(s[i]) == -1)
            {
                // character needs encoding
                byte[] characterBytes = Encoding.UTF8.GetBytes(s[i].ToString());
                for (int b = 0; b < characterBytes.Length; b++)
                {
                    sb.AppendFormat(CultureInfo.InvariantCulture,
                    "%{0:X2}",
                    characterBytes[b]);
                }
            }
            else
            {
                // character is allowed
                sb.Append(s[i]);
            }
        }

        return sb.ToString();
    }
}

NoEncode chars is a list of the permitted characters:

private const string NoEncodeChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~";

An impact of this encoding is that spaces must be encoded as %20 rather than plus. I was worried that each space would end up counting as three characters towards the 140 character limit. I tested this and it isn't true, so use HttpUtility.UrlEncode() to calculate the number of characters in the post OAuthUrlEncode() or similar to actually encode post parameter.

Tags: , ,

Categories: .NET | C#

 

CC | About | Search | Home | Validate