Search code examples
c#cocoamononstableviewmonomac

Implementing NSOutlineViewDataSource using MonoMac


I'm trying to implement a datasource for an NSOutlineView. The problem is that I don't know what type of object to return from outlineView:child:ofItem:.

Current code looks like this:

[Export("outlineView:child:ofItem:")]
public NSObject childOfItem(NSOutlineView outline, int child, NSObject item)
{
    return new MyItem();
}

With MyItem:

public class MyItem : NSObject
{}

EDIT: With this code, I get an InvalidCastException just after returning MyItem.


Solution

  • If you inherit a new type from NSOutlineViewDataSource then you should not re-export its outlineView:child:ofItem: selector on your own method. Instead what you should do is override the GetChild method that already export this selector, e.g.

    public overrride NSObject GetChild (NSOutlineView outlineView, int childIndex, NSObject ofItem)
    {
        return new MyItem ();
    }
    

    Note: that might not help since I have not tried it (I mostly do MonoTouch things) but do review other selectors you might be redefining/exporting in your application (to see if you should not be override-ing them from the base class you're inheriting from).