6

this is really throwing me for a loop. I installed Xamarin for Visual Studio, created an empty project and connected to my Mac. I simulate an iPhone and get this error

Foundation.MonoTouchException: Objective-C exception thrown.  Name: NSInternalInconsistencyException Reason: Application windows are expected to have a root view controller at the end of application launch

Which is being caused by the call to UIApplication.Main in the Main.cs class

using UIKit;

namespace testapp
{
    public class Application
    {
        // This is the main entry point of the application.
        static void Main(string[] args)
        {
            // if you want to use a different Application Delegate class from "AppDelegate"
            // you can specify it here.
            UIApplication.Main(args, null, "AppDelegate");
        }
    } 
}

I have absolutely nowhere to think to look - has anyone run into this before?

reZach
  • 8,945
  • 12
  • 51
  • 97
  • Does Xamarin give you a place to write code for your iOS target? We generally set up our window state within the AppDelegate's application:didFinishLaunchingWithOptions: method. I'm not sure what Xamarin provides, but thought that might get you something more interesting to search for. – Chris Trahey Feb 10 '17 at 03:22
  • 1
    Can you please share code from your AppDelegate.cs ? – PlusInfosys Feb 10 '17 at 07:45

5 Answers5

11

I faced the same error and what was wrong in my case is that I run async/await methods before MainPage was set. So, I just moved MainPage = new MainPage(); right after InitializeComponent(); and it works now.

Dmitry
  • 421
  • 4
  • 14
  • 2
    In my case, while the app was initializing, I was doing several thing in local database (e.g. : check if i have a AuthToken valid to skip authenticationPage). While I do stuff to find which page to start with, I've set the main page to a splashscreen like page. You can also fix this bug by doing MainPage = new Xamarin.Forms.ContentPage(); – Nk54 Oct 03 '18 at 08:53
  • 1
    Wow, what a crazy fix, never seen this in XF until today. – Jammer May 05 '21 at 15:12
5

It seams that AppDelegate is not implemented correctly in your project. iOS needs to know what´s the root controller (UIViewController) to init the app. The AppDelegate class is responsible for setting it. This will add an empty view controller at the app initialization:

[Register ("AppDelegate")]
public partial class AppDelegate : UIApplicationDelegate
{
    UIWindow window;

    public override bool FinishedLaunching (UIApplication app, NSDictionary options)
    {
        window = new UIWindow (UIScreen.MainScreen.Bounds);
        window.RootViewController = new UIViewController ();
        window.MakeKeyAndVisible ();

        return true;
    }
}
xleon
  • 6,201
  • 3
  • 36
  • 52
3

I had the same problem recently when importing an existing project.

Setting the MainPage in App.cs fixed it for me along with the changes mentioned for AppDelegate by xleon

MainPage = new ContentPage();
lvin
  • 290
  • 3
  • 11
0
    public override bool FinishedLaunching(UIApplication app, NSDictionary options)
    {
        bool returnValue = base.FinishedLaunching(app, options);
        helper.rootView = this.Window.RootViewController;
        return returnValue;
    }

That will enable you to run the base class FinishedLaunching method and get a result. Here helper is a class where I needed to know the rootView at app startup.

John Ernest
  • 785
  • 1
  • 8
  • 20
0

For Xamarin Forms: My problem seemed to be stemming from an await in LoadApplication() inside the app.xaml.cs which is strange because it was working yesterday, but today it decided that it wants to throw an exception at that await.