Random ArgumentException in a Windows Phone 7 application

While working on the last update of the 2Day todo-list application for Windows Phone, I encountered a strange bug I wanted to share in this post. The symptoms were very awkward: an ArgumentException where thrown randomly while using the app:

As always when I got that kind of stuff, I try to find out a reproducible set of actions which lead to the crash. Unfortunately in this case it wasn’t that easy.

After a couple of try, I found out I could reproduce the problem by doing the same set of actions many times:

  • launch 2Day
  • navigate to the details view of a task
  • go back to the home screen
  • navigate to the details view of another task
The crash then happened between 1 and 20 tries. The stacktrace wasn’t very helpful:
at MS.Internal.XcpImports.CheckHResult(UInt32 hr)
at MS.Internal.XcpImports.Collection_AddValue[T](PresentationFrameworkCollection`1 collection, CValue value)
at MS.Internal.XcpImports.Collection_AddDependencyObject[T](PresentationFrameworkCollection`1 collection, DependencyObject value)
at System.Windows.PresentationFrameworkCollection`1.AddDependencyObject(DependencyObject value)
at System.Windows.Controls.UIElementCollection.AddInternal(UIElement value)
at System.Windows.PresentationFrameworkCollection`1.Add(UIElement value)
at System.Windows.Controls.ItemsControl.AddVisualChild(Int32 index, DependencyObject container, Boolean needPrepareContainer)
at System.Windows.Controls.ItemsControl.AddContainers()
at System.Windows.Controls.ItemsControl.RecreateVisualChildren(IntPtr unmanagedObj)
at MS.Internal.XcpImports.MeasureOverrideNative(IntPtr element, Single inWidth, Single inHeight, Single& outWidth, Single& outHeight)
at MS.Internal.XcpImports.FrameworkElement_MeasureOverride(FrameworkElement element, Size availableSize)
at System.Windows.FrameworkElement.MeasureOverride(Size availableSize)
at Microsoft.Phone.Controls.Pivot.MeasureOverride(Size availableSize)
at System.Windows.FrameworkElement.MeasureOverride(IntPtr nativeTarget, Double inWidth, Double inHeight, Double& outWidth, Double& outHeight)
After some time spend on the Internet, I found out this post on StackOverflow. It basically told me something could be wrong with the naming of the elements in the XAML. Actually I did change some XAML in 2Day in order to change the layout of the task create/edit page:
During that refactoring I changed the way the notes are displayed and I created a UserControl for that purpose. Ultimately, I found out that this control was named: it has a x:Name set at the top of the XAML (note the line 8 in the following sample):
1
2
3
4
5
6
7
8
9
10
11
12
<UserControl x:Class="Japf.ToDo.Controls.NoteDisplayer"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
    mc:Ignorable="d"
    x:Name="control"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    d:DesignHeight="480" d:DesignWidth="480">
I needed that because I wanted to databind the Text property of a TextBox on a Dependency Property defined on the control itself:
1
<TextBox Text="{Binding ElementName=control, Path=Content, Mode=TwoWay}"/>

Because the post on StackOverflow talked about naming controls, I thought that could be the cause of the error. It it was actually ! Removing this single piece of XAML (and changing the binding a little bit) removed the crash from the app !

This is definitively the strangest bug I’ve seen on the Windows Phone platform… Hope it helps :-)

If you want to give a try to a fast & fluid todo-list app for Windows Phone 7, feel free to download 2Day:

7 thoughts on “Random ArgumentException in a Windows Phone 7 application

  1. Well, I must admit that naming root user control works, but only when you don’t name it’s instance in page’s xaml. In your case, it should be ok until you do following:

    ..

    ..

  2. >In your case, it should be ok until you do following:
    Part of my comment seems to be eaten by the blog.
    So, continuation

    .Page.

    .uc:NoteDisplayer x:Name=”firstNote” /.

    ./Page.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>