Framework Exceptions in Windows Phone

by Nick 6. January 2012 05:44

One of the switches that I often enable in Visual Studio is for it to notify my whenever an exception is thrown. This includes exceptions that are thrown internally by the .NET Framework. To enable this option, launch the Exceptions window from the Debug menu.

image

Select the Common Language Runtime Exceptions and check the Thrown checkbox. Click OK to apply this change

image

Now when you run your application you will see any exceptions that are raised, even if they are handles by you or the .NET Framework. Unfortunately sometimes this can be a bit of a drag because the .NET Framework does quite often throw exceptions, sometimes for legitimate reasons, sometimes not so. One such case is for both the CheckBox and RadioButton (actually the ToggleButton which is the base control is the source of this issue). After enabling Exceptions, add the following xaml to a new project.

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
    <CheckBox Content="Checkbox 1" />
</Grid>

Run your application and you will see the following, completely meaningless, exception being raised.

image

If you look at the call stack, apparently something is calling ToString on the ToggleButton control (ie the base control for the CheckBox you just added).

image

If you examine the ToString code, you’ll see something similar to the following, and you can see the reference to Resx.GetString. From the callstack this seems to be the issue.

public override string ToString()
{
    string text = base.ToString();
    string text2 = (base.Content ?? "").ToString();
    bool? isChecked = this.IsChecked;
    return string.Format(CultureInfo.InvariantCulture, Resx.GetString("ToggleButton_ToString_FormatString"), new object[]
    {
        text,
        text2,
        isChecked.HasValue ? isChecked.Value.ToString() : "null"
    });
}

Following this even further you eventually get to the internal Resx constructor where you can see that it attempts to load an assembly (see second line of the above callstack). This is the line that is failing, because the System.Windows.debug.resources assembly doesn’t exist for us developers Sad smile

internal Resx()
{
    Assembly assembly = base.GetType().Assembly;
    this.resources = new ResourceManager("System.Windows", assembly);
    string assemblyString = "System.Windows.debug.resources, Version=2.0.5.0, Culture=en-US, PublicKeyToken=7cec85d7bea7798e";
    try
    {
       Assembly assembly2 = Assembly.Load(assemblyString);
        this.debugResources = new ResourceManager("System.Windows.debug", assembly2);
    }
    catch (FileNotFoundException)
    {
    }
    this.fallbackResources = new ResourceManager("mscorlib", typeof(object).Assembly);
}

 

The work around for this is relatively simple. Create your own Checkbox class that inherits from CheckBox and override the ToString method.

public class MyCheckBox:CheckBox
{
    public override string ToString()
    {
        return string.Empty;
    }
}

Add this to your layout in place of the CheckBox

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
    <PhoneApp23:MyCheckBox Content="Checkbox 1" />
</Grid>

Run: Look, no Exceptions!

NOTE: Whilst the fact that the exception is thrown in the first place probably isn’t great coding, it has little, if any, effect on your app running in production. I’ve described this work around purely to get around the annoyance of the FileNotFoundException being picked up (the other way would be to specifically exclude this type of exception in the Exceptions dialog). Some ad-hoc testing indicates that this exception has an insignificant effect on performance or rendering times.

Tags:

Off Topic | Mobile

Sydney Hobart Yacht Race and the Lack of Sailing Apps

by Nick 27. December 2011 17:25

Recently I enjoyed watching the highlight from the Perth 2011 ISAF Sailing World Championships but was disappointed in the lack of mobile accessibility for results and information about the event. Whilst the website was functional, and actually looked quite good if you were viewing it on a desktop/laptop computer, it left a lot to be desired when accessing it via a mobile device. It seems surprising that given the push by the sailing fraternity to raise the profile of the sport that there was no consideration given to building an application for any of the mobile platforms.

The second failure of the sailing community is this year's Rolex Sydney Hobart Yacht Race. Personally I’m not a big fan of the design of the website but I do like the Yacht Tracker system they’re using. Unfortunately again there are no apps available for any of the mobile platforms as far as I’m aware.

Yesterday after watching the start to the Sydney Hobart on TV (yes, even though I live in Sydney I actually think you get a better view of the start via TV that fighting the crowds at either of the heads) I decided that I would put together a quick Windows Phone application using the data exposed by Twitter, the RSS feed and the data feeds that Yacht Tracker uses. I haven’t bothered to submit the app to Marketplace as the race would be over before it gets approved. Unfortunately this means that only users with an unlocked/developer Windows Phone can use the app.

My apologies if you can’t run the app, send me an email via the website and I’ll see what I can do to get you a trial version. I’ve also included the following screenshots so you can see what you’re missing (remember, this is less than 8 hours of work!)

sh1 s2 s3 s4

 

Updated 28/12/2011 00:20: Now supports background agent support for updating primary live tile with race leaders.

Updated 28/12/2011 11:13: Added multi-tile support, boat details, full page map support and about page.

Tags:

Mobile | Off Topic

Android Frustrations

by Nick 7. April 2011 07:49

This evening I spent a number of hours chasing my tail.  All I wanted to do was to debug the very simple Android application. I figured all I would have to do is go ahead and purchase MonoDroid (you can only debug using the emulator with the evaluation license, which is painfully slow and periodically breaks). Unfortunately this was not the case.

It turns out that my Samsung Galaxy S which is less than a year old is running Android v2.1 which is too old for the MonoDroid toolkit. I then spent the next hour fighting Samsungs poor excuse for a desktop application, Kies. It doesn’t help that the new version (v2) isn’t compatible with their less than a year old devices. I then managed to locate an older version (v1.5) which was compatible but for the life of me I couldn’t get my device to connect to the software. Finally after tweaking all sorts of things I got it to connect only to discover that the software didn’t have an update for my Galaxy S. ARGH.

Ok, back to basics – lets just get a cooked ROM and apply that. Sure enough I found instructions on how to a) root my Galaxy S and then to b) install Android 2.3.3:

How to Root Samsung Galaxy S

http://www.pathikshah.com/blog/root-samsung-galaxy-s/

Install Android 2.3.3 Gingerbread on Samsung Galaxy S

http://www.pathikshah.com/blog/install-android-2-3-3-gingerbread-on-samsung-galaxy-s/

After doing all that I now have a device I can debug to from Visual Studio 2010 using MonoDroid. This is way way way too painful, but to be expected from Google and Android.

Tags: , ,

Off Topic

Powered by BlogEngine.NET 2.0.0.36

Automotive Theme by Car Leasing Experts

 

Page List