Xamarin.Forms 应用因 InitializeComponent 中的 FileNotFoundException 而崩溃

Xamarin.Forms app crashes with FileNotFoundException in InitializeComponent

我的 Xamarin.Forms 应用程序在我打开特定页面时总是崩溃。崩溃是可重现的,但仅限于发布模式。当我在调试模式下构建和 运行 应用程序时,同一页面可以正常打开。

通过一些努力,我设法捕获异常并在关闭应用程序之前在消息 window 中显示堆栈跟踪。当我的页面调用 InitializeComponent方法:

System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime' or one of its dependencies
File name: 'System.Runtime'
  at System.AppDomain.Load (System.Reflection.AssemblyName assemblyRef, System.Security.Policy.Evidence assemblySecurity)
  at System.AppDomain.Load (System.Reflection.AssemblyName assemblyRef)
  at (wrapper remoting-invoke-with-check) System.AppDomain:Load (System.Reflection.AssemblyName)
  at System.Reflection.Assembly.Load (System.Reflection.AssemblyName assemblyRef)
  at Xamarin.Forms.Xaml.XamlParser.GetElementType (Xamarin.Forms.Xaml.XmlType xmlType, IXmlLineInfo xmlInfo, System.Reflection.Assembly currentAssembly, Xamarin.Forms.Xaml.XamlParseException& exception)
  at Xamarin.Forms.Xaml.CreateValuesVisitor.Visit (Xamarin.Forms.Xaml.ElementNode node, INode parentNode)
  at Xamarin.Forms.Xaml.ElementNode.Accept (IXamlNodeVisitor visitor, INode parentNode)
  at Xamarin.Forms.Xaml.RootNode.Accept (IXamlNodeVisitor visitor, INode parentNode)
  at Xamarin.Forms.Xaml.XamlLoader.Visit (Xamarin.Forms.Xaml.RootNode rootnode, Xamarin.Forms.Xaml.HydratationContext visitorContext)
  at Xamarin.Forms.Xaml.XamlLoader.Load (System.Object view, System.String xaml)
  at Xamarin.Forms.Xaml.XamlLoader.Load (System.Object view, System.Type callingType)
  at Xamarin.Forms.Xaml.Extensions.LoadFromXaml[TXaml] (Xamarin.Forms.Xaml.TXaml view, System.Type callingType)
  at MyApp.MyNamespace.Pages.MyPage.InitializeComponent ()
  at MyApp.MyNamespace.Pages.MyPage..ctor ()

我的代码有很多 System.Runtime 相关的导入,但是指示问题位于 XAML 中的堆栈跟踪让我找到了真正的问题。

问题是该页面上的通用视图,其中类型参数是 "system:String"。

Resharper auto-completed 导入 System.Runtime 程序集:

 <mp:MyPage x:Class="MyApp.MyNamespace.Pages.MyPage"
             xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:mc="clr-namespace:MyApp.MyNamespace.Controls;assembly=MyApp"
             xmlns:mp="clr-namespace:MyApp.MyNamespace.Pages;assembly=MyApp"
             xmlns:system="clr-namespace:System;assembly=System.Runtime">
   <!-- ... -->
   <mc:MyControl x:TypeArguments="system:String" />
   <!-- ... -->
 </mp:MyPage>

虽然出于某种原因这在调试模式下有效,但在发布模式下,该声明会导致应用程序在您创建页面时崩溃并出现上述错误。当我将程序集更改为 mscorlib 时,该应用程序在调试 发布模式下运行:

 <mp:MyPage x:Class="MyApp.MyNamespace.Pages.MyPage"
             xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:mc="clr-namespace:MyApp.MyNamespace.Controls;assembly=MyApp"
             xmlns:mp="clr-namespace:MyApp.MyNamespace.Pages;assembly=MyApp"
             xmlns:system="clr-namespace:System;assembly=mscorlib">
   <!-- ... -->
   <mc:MyControl x:TypeArguments="system:String" />
   <!-- ... -->
 </mp:MyPage>