有关引用其他工程UserControls造成XamlParseException异常的问题

今天将之前写的一个模块移植到了Windows Store App平台的项目上。模块中有UserControl,也有Page。通过Activator生成一个UserControl实例的时候出现了异常:

“System.Reflection.TargetInvocationException”类型的异常在 mscorlib.dll 中发生,但未在用户代码中进行处理

其他信息: Exception has been thrown by the target of an invocation.

查看InnerException,发现是XAML parsing failed。感觉很奇怪,因为还没有添加到主解决方案时候测试是好用的。不仅如此,直接Frame.Navigate()到Page,也会引发异常,这次直接是XamlParseException了,依然是XAML parsing failed。

抱着试试看的心理,把用到的UserControl整个从界面到code behind都注释掉了,还是有异常。再试,新建一个UserControl,还是有。

那么问题肯定出在模块内的UserControl上了,从this.InitializeComponent();跟进去,看到了奇怪的代码:

global::Windows.UI.Xaml.Application.LoadComponent(this, new global::System.Uri("ms-appx:///Carta_UiComponents/LazyLoadListControl.xaml"), global::Windows.UI.Xaml.Controls.Primitives.ComponentResourceLocation.Nested);

“Carta_UiComponents”,这里程序集名称中的点号被翻译成了下划线。就算手动改成点号也没有,因为.g.i.cs属于codgen文件,自动生成。放狗一搜,发现之前就有不少人碰到过相同的问题:Migrating Windows RP application to Windows 8 RTM – XAMLParseException关于Visual Studio 2012 RTM 中创建windows 8 style类型的应用出现的XamlParseException 异常http://blog.excastle.com/2012/09/06/xamlparseexception-in-winrt/,而且都提到在微软给出的Hotfix页面上,实际上并没有patch下载,所以目前来说最简单的解决方案就是——新建项目后把默认程序集名字里面的点都去掉,WTF!