How To Overcome Windows Phone 8 XAML Compilation Errors
I’ve been working on converting our Windows Phone 7 app to use the Windows Phone 8 SDK for the past 1.5 months, and it’s been quite the struggle. No matter what I did, I couldn’t get any Windows Phone 8 projects to compile! They all returned the same error:
“Cannot resolve reference assemblies. Please check the reference assemblies. Could not load file or assembly ‘System.Core, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e’ or one of its dependencies. The located assembly’s manifest definition does not match the assembly reference.”
It was quite the connundrum. So I cracked out my pipe, put on my tweed jacket and ruminated as to what might be happening here. The first clue I had was that the XAML files were looking for a reference of the System.Core.dll with the version 2.0.5.0. System.Core is the base .NET assembly which is expected, but what is off is the version number. Windows Phone 8 is built off some derivation of the .NET 4.0 framework, so the System.Core file it should expect to use should have a version of 4.0.x or something.
Assembly binding errors are the bane of any .NET developer’s existence and once again I was confronted by it.
I checked the Global Assembly Cache to see what version of System.Core was registered and I only had the .NET 4.0.x version. I checked the XAML file to ensure there wasn’t any errant references to the older System.Core assembly. The XAML looked like this:
</pre> <phone:PhoneApplicationPage x:Class="HTML5App1.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" FontFamily="{StaticResource PhoneFontFamilyNormal}" FontSize="{StaticResource PhoneFontSizeNormal}" Foreground="{StaticResource PhoneForegroundBrush}" SupportedOrientations="Portrait" Orientation="Portrait" shell:SystemTray.IsVisible="True"> <!--LayoutRoot is the root grid where all page content is placed--> <Grid x:Name="LayoutRoot" Background="Transparent"> <phone:WebBrowser x:Name="Browser" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Loaded="Browser_Loaded" NavigationFailed="Browser_NavigationFailed" /> </Grid> <!-- ApplicationBar --> <phone:PhoneApplicationPage.ApplicationBar> <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True" Mode="Minimized"> <shell:ApplicationBarIconButton IconUri="/Assets/AppBar/appbar.back.rest.png" IsEnabled="True" Text="back" Click="BackApplicationBar_Click"/> <shell:ApplicationBarIconButton IconUri="/Assets/AppBar/appbar.next.rest.png" IsEnabled="True" Text="forward" Click="ForwardApplicationBar_Click"/> <shell:ApplicationBar.MenuItems> <shell:ApplicationBarMenuItem Text="home" Click="HomeMenuItem_Click" /> </shell:ApplicationBar.MenuItems> </shell:ApplicationBar> </phone:PhoneApplicationPage.ApplicationBar> </phone:PhoneApplicationPage> <pre>
It’s clean, no errant references.
Still stumped, I decided it was time to take the gloves off and start building my project at the command line. So I opened up the Visual Studio 2012 Command Line, and ran a MSBuild.exe with this command:
MSBuild.exe HTMLPhoneApp1.csproj /v:diagnostic /fl1
This gave me a full verbose dump of every step in the compilation process. Buried in the middle of this log I found the error being thrown during the build step with the name “ValidateXAML”. I inspected the Microsoft.WindowsPhone.Common.targets file under “C:Program Files (x86)MSBuildMicrosoftWindowsPhonev8.0”, but that all looked fine.
Then I remembered what a Winston Churchil had said to me in a dream I had many years ago:
“Son, when life gives you XAML validation errors, turn off XAML validation.”
Brilliant! So I cracked open the .csproj file in a XML editor and edited the ValidateXaml property to ‘false’.
And voila! Everything compiled and I was off. I’ve now built and debugged my old app in Windows Phone 8 and everything is working on the device and emulator. Now, this isn’t a real solution to the root cause, but it’s a workaround to get you moving again if you are stuck like I was.
TL;DR
If your Windows Phone 8 project fails during XAML compilation due to incorrect assembly references, turn off XAML validation in the .csproj file to get around the issue.
Bobby Gill