Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin iOS root view controller at the end of application launch

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?

like image 243
reZach Avatar asked Feb 10 '17 02:02

reZach


3 Answers

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.

like image 135
Dmitry Avatar answered Nov 10 '22 18:11

Dmitry


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;
    }
}
like image 5
xleon Avatar answered Nov 10 '22 18:11

xleon


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();
like image 3
lakvin Avatar answered Nov 10 '22 17:11

lakvin