We have a Xamarin.iOS project that shares a code backend with a WPF project that has been going through a refactor. All the shared code libraries have been converted to be .net standard.
The Xamarin project builds, but the simulator throws an exception on startup as such :
Foundation.MonoTouchException has been thrown
Objective-C exception thrown. Name: NSInternalInconsistencyException Reason: Unable to instantiate the UIApplication delegate instance. No class named AppDelegate is loaded.
Just for testing purposes, I added an empty UIApplicationDelegate to Main.cs and I'll still get the exception (only now referring to TestDelegate).
Main.cs
using Foundation;
using UIKit;
namespace App.IOS
{
public class Application
{
static void Main (string[] args)
{
UIApplication.Main (args, null, nameof(AppDelegate));
}
}
}
Appdelegate.cs (very large file, so just the declarations here).
namespace App.IOS
{
public class MiniSetup
{
public static readonly MiniSetup Instance = new MiniSetup ();
public void EnsureInit()
{
//Setting up Binding and Dependency Injection Here.
}
}
[Register (nameof(AppDelegate))]
public partial class AppDelegate : UIApplicationDelegate
{
public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
//Invoke Minisetup here
}
public override void WillEnterForeground (UIApplication application)
{
//snip
}
public override void DidEnterBackground (UIApplication
application)
{
//snip
}
// snip other code for setting UI controls and other app logic.
}
It seems apparent that I've goofed up the project in some way, but I haven't found any leads as to where to look for a solution. Any ideas on how to troubleshoot this?
Two classed needed, the UIApplicationDelegate
subclass and the class that defines the Main
entry point that calls UIApplication.Main
will the Xamarin.iOS
registered name that you used on the UIApplicationDelegate
subclass.
Basic/Minimum UIApplicationDelegate:
[Register("AppDelegate")]
public class AppDelegate : UIApplicationDelegate
{
public override UIWindow Window
{
get;
set;
}
}
Basic/Minimum Main entry point:
public class Application
{
static void Main(string[] args)
{
UIApplication.Main(args, null, "AppDelegate");
}
}
I don't have a smoking gun here, but near as I was able to figure out, an incompatible (with xamarin) reference or nuget package was added to the IOS project. While the native code was throwing a native assembly, I'd imagine that AppDelegate was actually throwing a NotSupportedException or an AssemblyLoader Exception or similar.
So removing all references and nuget packages, then re-adding just the bare minimum seems to have resolved this issue.
Rebuild your solutions then restart your IDE.
Nothing stands out as a problem in the code you have posted.
However, you can try the following :
- AppDelegate code is missing a 'Window' property. If you have just not added it to your code snippet in the question for brevity, fine. If not, do add it to your app delegate. Both Xamarin and Apple mention this in their documentation for ApDelegate and Storyboards.
public override UIWindow Window { get; set; }
Make sure the Build action
for you AppDelegate is set to Compile
and not mistakenly changed to none
or any other value. This is highly unlikely, but because you mentioned you're going through a huge refactor, this could have been changed accidentally.
Similar problems were reported with different Xamarin.iOS versions before. So try running you project with a different xamarin version setup.
Although, i presume, you've tried this multiple times before posting the question, i'd mention : try a complete clean and rebuild. (Manually delete the bin' and
obj` folders in your iOS project folder, clean the trash, and then do a rebuild just to be sure. Also, try out a full machine restart, remember you're using a Microsoft product!)
I also found this weird line in the same Xamarin documentation, although i am not sure how this can be checked, because the storyboards do not have an owner/AppDelegate reference.
Application's that are launched from a XIB or storyboard use the
UIApplicationDelegate specified in the XIB or storyboard.
Hope either of these steps helps you out. Good luck.
Set the Build Action property of AppDelegate.cs
to Compile.
For me it was some missing reference in the iOS
project.
Make sure that whatever the Shared
and the iOS
projects reference at least the same packages.