Saturday, 15 January 2011

Windows Phone Development and Designer Exceptions

I've been building a few Windows Phone applications recently and have been learning lot's along the way and am hopefully releasing a couple of these in the near future. However on my current project I have been constantly hounded by the designer view of my xaml pages throwing exceptions.


Tonight I decided to finally look into why these occur, I have managed to ignore them previously due to the weird nature of them. My exception scenario consists of a page that contains a pivot control and inside one of the pivot items there is a user control. The user control contains a simple list view which has some data bound to it

Now I have always got past the exceptions as if you use the designer view for the user control it works fine, however as soon as I put it into a page or a pivot the designer starts kicking off.


The actual exception is as follows:

Could not load type 'System.Net.HttpUtility' from assembly 'System.Windows, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e'.
It then goes on to tell me the line number and location etc of this. At first glance this exception looks a bit weird as in the code view and if you run the application the exception is never thrown.

The first thing I did to try and resolve this issue was to find where System.Net.HttpUtility lives, in the desktop version of Silverlight it lives in System.Windows.Browser But as the Windows Phone version of Silverlight doesn't include System.Windows.Browser the development team moved the httputility type into the System.Net namespace and put it into the System.Windows assembly, http://msdn.microsoft.com/en-us/library/dd470087(v=vs.95).aspx#Assemblies. This explains why the project builds and runs etc, however it doesn't explain why the designer has such a bad time with it.


Sadly I couldn't find a source or information on this exception, my guess is the designer is loading an older version of the assembly, a missing designer tool update from the RTM or something similar {Edit see my update below}. However what I have found is that Silverlight does have another way of encoding and decoding URL strings. System.Uri.EscapeUriString, if you use this the phone and the designer all work happily. A minor change and a headache eased :) I'm hoping the next tools update fixes the issue but until then this seems the safest method of escaping url strings.

Update

As I oddly found this issue intriguing I decided to investigate further. I opened another Visual Studio attached it to the Visual Studio process I had my project in and turned on catch all exceptions. Then when the exception was thrown I looked at the AppDomains assembly list, System.AppDomain.CurrentDomain.GetAssemblies(), 55 in all were loaded. I then looked through this list for the assembly referenced in the exception. This assembly then had the following location value: Location "c:\\Program Files (x86)\\Microsoft Silverlight\\4.0.51204.0\\System.Windows.dll". This is the main Silverlight 4 runtime, NOT the Windows Phone version which is 3.7.x, to be certain I then opened the assembly in Reflector to be sure the System.Net.HttpUtility was missing and it was.

So as suspected the designer is loading the wrong assembly thus the error, the assembly it should be loading is in the referenced assemblies folder, C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\Silverlight\v4.0\Profile\WindowsPhone\System.Windows.dll , hopefully reporting this on Connect will result in a hotfix / update.

2 comments:

Hossein Mayboudi said...

WEB PI INSTALLER :(

OS Version = 6.1.7601, Platform 2, Service Pack 1
OS Description = Windows 7 - x64 Enterprise Edition Service Pack 1
CommandLine = c:\d77efd4cd4287177490513db60714e1f\Setup.exe
TimeZone = Iran Daylight Time
Initial LCID = 1033
Using Simultaneous Download and Install mechanism
Operation: Installing
Package Name = KB2483190
Package Version = 10.0.30319
User Experience Data Collection Policy: UserControlled
Number of applicable items: 1
Summary Information:

Microsoft Visual Studio 2010 Ultimate - ENU


Patch (c:\d77efd4cd4287177490513db60714e1f\VS10-KB2483190.msp) Install failed on product (Microsoft Visual Studio 2010 Ultimate - ENU). Msi Log:
Final Result: Installation failed with error code: (0x80070643), "Fatal error during installation. " (Elapsed time: 0 00:00:20).

Todd Baginski said...

Thanks! This worked like a charm for me.

I was using this code:

result = HttpUtility.HtmlDecode(result);

and after reading your post I replaced it with this code in my WP7 app and now my designer errors have gone away.

result = System.Uri.UnescapeDataString(result);

Thanks!