As part of building Windows Phone applications I’m forever flipping between Visual Studio and Expression Blend. Sometimes I encounter issues with Blend that it just can’t deal with and will simply self combust, giving me a text file crash stack. The most common cause is where I’ve accidentally crossed threads, resulting in the following:
System.UnauthorizedAccessException: Invalid cross-thread access.
at MS.Internal.XcpImports.CreateObjectByTypeIndex(UInt32 typeIndex)
at System.Windows.Threading.DispatcherTimer..ctor(UInt32 nativeTypeIndex)
Unfortunately the resolution isn’t that simple – I can fix the code ok, but the issue is that Blend won’t load the project again without crashing. This is because Blend uses a compiled version of the application to display the design surface. On startup it attempts to use the version that has already been built (in this case it includes the faulty code that causes the exception) so it crashes again. Unlike Visual Studio that handles this gracefully, there is no capability within Blend to for example open the project without any files open….
The solution: Delete the bin/obj folders from your application and open in Blend (of course after fixing the code issue). Blend will then be forced to do a new build of the application that should no longer raise the exception.