Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to subclass UIApplication in Monotouch?

EDIT:

A couple of years later, things are easier. It is now possible to omit the Register() attributes, both on the application and the app delegate and instead use:

UIApplication.Main(args, typeof(CustomApp), typeof(CustomAppDelegate));


In order to be able to override UIApplication.SendEvent() I want to subclass UIApplication:

public class UIApplicationMain : UIApplication
{
    public UIApplicationMain () : base()
    {
    }

    public override void SendEvent (UIEvent uievent)
    {
        base.SendEvent (uievent);
    }
}

In the main.cs I use this code:

public class Application
{
    static void Main (string[] args)
    {
        UIApplication.Main (args, "UIApplicationMain", "AppDelegateBase");
    }
}

But it fails with:

Objective-C exception thrown.  Name:

NSInternalInconsistencyException Reason: Unable to instantiate the UIApplication subclass instance. No class named UIApplicationMain is loaded.

So I'm missing some attributes I guess. But what and where?

like image 373
Krumelur Avatar asked Sep 30 '11 11:09

Krumelur


1 Answers

Add a [Register] attribute to your new type, like:

 [Register ("UIApplicationMain")]
 public class UIApplicationMain : UIApplication {
    ...

That will allow the native side to instantiate the right type when Main gets executed.

like image 185
poupou Avatar answered Oct 04 '22 15:10

poupou