Search code examples
xamlxamarin.formsinitializecomponent

Xamarin.Forms app crashes with FileNotFoundException in InitializeComponent


My Xamarin.Forms app keeps crashing when I open a specific page. The crash is reproducible, but only in Release mode. When I build and run the app in Debug mode, the same page opens fine.

With some effort I managed to catch the exception and display the stack trace in a message window before the app is closed. The core error seems to be a FileNotFoundException for the System.Runtime assembly somewhere in the XamlParser of the XamlLoader that runs when my page calls the InitializeComponent method:

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

Solution

  • My code has a lot of System.Runtime related imports, but the stack trace indicating the problem to be located in the XAML allowed me to find the real issue.

    The problem was a generic view on that page, where the type argument was "system:String".

    Resharper auto-completed this to an import of the System.Runtime assembly:

     <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>
    

    While this works in debug mode for some reason, in release mode that declaration causes the app to crash with the mentioned error when you create the page. When I changed the assembly to mscorlib, the app worked in Debug and Release mode:

     <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>