Notice (2018-05-24): bugzilla.xamarin.com is now in
Please join us on
Visual Studio Developer Community and in the
Mono organizations on
GitHub to continue tracking issues. Bugzilla will remain
available for reference in read-only mode. We will continue to work
on open Bugzilla bugs, copy them to the new locations
as needed for follow-up, and add the new items under Related
Our sincere thanks to everyone who has contributed on this bug
tracker over the years. Thanks also for your understanding as we
make these adjustments and improvements for the future.
Please create a new report on
Developer Community or GitHub with
your current version information, steps to reproduce, and relevant error
messages or log files if you are hitting an issue that looks similar to
this resolved bug and you do not yet see a matching new report.
I am trying to use bindings to bind an NSTreeController to a NSOutlineView. I have run across a problem. When I try to get the RepresentedObject, I get an InvalidCastException. I don't see a way to work around this, so I am stuck.
Here is is some sample code that causes the exception:
var treeNode = NSTreeNode.FromRepresentedObject (new NSObject ());
var representedItem = treeNode.RepresentedObject;
I'd be glad to dig deeper, but I am not finding the source code for NSTreeNode on github.
Let me know what else I can do to help.
Here is what I am running. Not exactly sure how to tell what version of MonoMac I am running though.
=== Xamarin Studio ===
Version 4.0.11 (build 6)
Installation UUID: 7de50ea0-5da0-4937-9d47-d071aa496e85
Mono 3.2.1 ((no/f3f789e)
Package version: 302010001
=== Apple Developer Tools ===
Xcode 4.6.3 (2068)
=== Xamarin.iOS ===
=== Xamarin.Android ===
=== Xamarin.Mac ===
Xamarin.Mac: Not Installed
=== Build Information ===
Release ID: 400110006
Git revision: b8a862fc80adef69bb383dd305619186da9e1f9b
Build date: 2013-07-31 09:26:05+0000
Xamarin addins: e0bfb020a2d4ed62aadd4d6f40bd83dcbac98ae3
=== Operating System ===
Mac OS X 10.8.4
Darwin freyja.lechnology.com 12.4.0 Darwin Kernel Version 12.4.0
Wed May 1 17:57:12 PDT 2013
Here is the stack trace.
System.InvalidCastException: Cannot cast from source type to destination type.
at MonoMac.AppKit.NSTreeNode.get_RepresentedObject () <IL 0x00027, 0x000b9>
at KeePass2Mac.GroupOutlineViewDelegate.GetView (MonoMac.AppKit.NSOutlineView,MonoMac.AppKit.NSTableColumn,MonoMac.Foundation.NSObject) [0x0000d] in /Users/david/src/KeePass2Mac/KeePass2Mac/GroupOutlineViewDelegate.cs:44
at (wrapper dynamic-method) object.[KeePass2Mac.GroupOutlineViewDelegate.MonoMac.AppKit.NSView GetView(MonoMac.AppKit.NSOutlineView, MonoMac.AppKit.NSTableColumn, MonoMac.Foundation.NSObject)] (MonoMac.Foundation.NSObject,MonoMac.ObjCRuntime.Selector,MonoMac.AppKit.NSOutlineView,MonoMac.AppKit.NSTableColumn,MonoMac.Foundation.NSObject) <IL 0x0001d, 0x00083>
at (wrapper native-to-managed) object.[KeePass2Mac.GroupOutlineViewDelegate.MonoMac.AppKit.NSView GetView(MonoMac.AppKit.NSOutlineView, MonoMac.AppKit.NSTableColumn, MonoMac.Foundation.NSObject)] (MonoMac.Foundation.NSObject,MonoMac.ObjCRuntime.Selector,MonoMac.AppKit.NSOutlineView,MonoMac.AppKit.NSTableColumn,MonoMac.Foundation.NSObject) <IL 0x000ab, 0x00267>
at (wrapper managed-to-native) MonoMac.AppKit.NSApplication.NSApplicationMain (int,string) <0x00003>
at MonoMac.AppKit.NSApplication.Main (string) <IL 0x00041, 0x00103>
at KeePass2Mac.MainClass.Main (string) [0x00007] in /Users/david/src/KeePass2Mac/KeePass2Mac/Main.cs:35
The problem is, the return type of representedObject is incorrectly specified as NSTreeNode, rather than NSObject.
I made a pull request to fix that here: https://github.com/mono/monomac/pull/116
If you are using monomac, you can merge that fix and just recompile.
If you are using Xamarin.Mac, you will have to wait for this to be merged and released. A workaround in the meantime could be to get the right behaviour using an extension method, e.g.
public static class Ext
static readonly IntPtr selRepresentedObjectHandle = Selector.GetHandle("representedObject");
public static NSObject GetRepresentedObject(this NSTreeNode treeNode)
ret = Runtime.GetNSObject(MonoMac.ObjCRuntime.Messaging.IntPtr_objc_msgSend(treeNode.Handle, selRepresentedObjectHandle));
var representedItem = treeNode.GetRepresentedObject();
Is this fixed in XamMac? I'm still seeing the issue.
It is part of the upcoming release.