1

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

经过一番努力,我设法在应用程序关闭之前捕获了异常并在消息窗口中显示堆栈跟踪。核心错误似乎是我的页面调用该方法时运行的某处FileNotFoundException的程序集:System.RuntimeXamlParserXamlLoaderInitializeComponent

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 ()
4

1 回答 1

2

我的代码有很多System.Runtime相关的导入,但是表明问题位于 XAML 中的堆栈跟踪使我能够找到真正的问题。

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

Resharper 自动完成了这个到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>
于 2017-01-26T22:32:02.740 回答