“System.IO.FileNotFoundException” using controls from another assembly in Xamarin Forms on iOS.

I was building a Xamarin solution with my components like that:

ProjectList

All my components (PSC.Xamarin.Controls.*) are working fine in other solutions.Always fine for Windows or UWP solutions. A problem was born when I started to deployed my solutions on iOS. When on the app I opened a page with my controls I always received an error like:

FileNotFoundException

System.IO.FileNotFoundException: Could not load file or assembly ‘PSC.Xamarin.Controls.BindablePicker’ or one of its dependencies. The system cannot find the file specified.

I can check my references and deployment settings in all ways, it turns out there’s probably a bug in Xamarin Forms’ iOS implementation. If you use controls and only controls from a PCL or dll that does not contain other code that is called from the app, apparently Xamarin on iOS ‘forgets’ to load/deploy the assembly (or something like that). But there is a work around.

The work around is as simple as it is weird. You go the iOS app’s AppDelegate class, and you add a reference to your class(es). If this is not enough, in the FinishedLaunching function you have to add a new instance to your control as in the following example:

using System;
using System.Collections.Generic;
using System.Linq;
using PSC.Xamarin.Controls.BindablePicker;

using Foundation;
using UIKit;

namespace myInventories.iOS {
    [Register("AppDelegate")]
    public partial class AppDelegate : 
        global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate {
        public override bool FinishedLaunching(UIApplication app, 
            NSDictionary options) {
            global::Xamarin.Forms.Forms.Init();

            // initialize my components
            // this is a work around only for iOS. 
            // If you don't do that, you received an error like 
            // System.IO.FileNotFoundException
            BindablePicker temporary = new BindablePicker();

            Xamarin.FormsMaps.Init();
            LoadApplication(new App());

            return base.FinishedLaunching(app, options);
        }
    }
}

Happy coding!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.