Framework Exceptions in Windows Phone

Framework Exceptions in Windows Phone

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.

SocialViewer gets Windows Phone Mango treatment

SocialViewer gets Windows Phone Mango treatment

As some of you may be aware I was involved in a project last year to enable developers to quickly build reading style applications. This started off as a simple template allowing users to pull in RSS data but exploded to be much more versatile. The template was made available at http://socialviewer.codeplex.com and is freely available for any Windows Phone developer to download and use.

Since the initial version we’ve made a number of enhancements to both functionality and the default look and feel. Over the festive season I started transitioning (and upgrading) the template to take advantage of the new Mango features. The configuration file (where you could previously only configure the feeds and lists for the application) now allows you to control layout, Ads and integration with various social networks.

I’m going to be doing a series of posts over the coming weeks on some of the new features of the Social Viewer template. We’ll start today with a recap of getting started with the template.

Step 1: Download the template

Go to http://socialviewer.codeplex.com and download the latest build from the Source Code tab. IMPORTANT: At this stage the latest version is still an Alpha release, which means that it’s probably not suitable for publishing apps. We’re hoping to get an official release out this month and would love any feedback you have.

Step 2: Unblock the Download

Gotta love Windows security – make sure you right-click the downloaded file, select Properties and then click the Unblock button.

image

Step 3: Extract the Download

Extract all the files from the changeset. There should be two files: a Zip file which is the template (don’t extract this file), and an OUT OF DATE word document that talks about the template (which for the time being you can ignore).

Step 4: Installing the Template

Copy the Zip file that was extracted out of the changeset (should be called BuiltToRoam.SocialViewer.Template.Zip) and place this file into your Visual Studio Templates folder. For example my templates folder is the following. Note that I added the “Silverlight for Windows Phone” sub-folder so that the template appears with all the other Windows Phone project templates.

C:UsersNickDocumentsVisual Studio 2010TemplatesProjectTemplatesVisual C#Silverlight for Windows Phone

Step 5: Run Visual Studio

Visual Studio should automatically pick up the new template even if it is already running. However, if you’re updating from a previous version of the SocialViewer template, you may need to restart Visual Studio for it to pick up the new version.

Step 6: Create a New SocialViewer Project

File –> New –> Project

Select the SocialViewer template and give the project a new name. The is currently a bug that means if you put a space in your project name you’re going to enter a word of pain. DO NOT PUT SPACES IN PROJECT NAME

image

Step 7: Run

That’s it…. out of the box you should literally be able to just run the newly created project. Please do not simply create a new project and publish it via Marketplace. That’s not the intent!

Here are some screen shots of the latest build:

image image image image

Main panorama: What’s new – vertical list of new feed items; Recent – hubtiles with images from Flickr; link list to pivot page, website (ie built to roam) and the about page.

image image image

On demand pivot (sources are only downloaded when the pivot is accessed). Reading page illustrating two different layouts: The default; WebBrowser for RSS feed items

image image

The Settings and About pages. Note the long list of social providers that can be used within your applications (See the configuration file) and the variety of data sources that you can consume (again, see configuration file).