Using the info from these SO answers:
I've come up with the following:
using System;
using AppKit;
using CoreGraphics;
using Foundation;
namespace Sandbox
{
public partial class ViewController : NSViewController
{
public static CGPoint Origin = new CGPoint(10, 10);
public static CGSize ButtonSize = new CGSize(80, 20);
public static CGSize ItemSize = new CGSize(100, 40);
public NSCollectionView CollectionView;
public NSString[] Labels = { new NSString("Volvo"), new NSString("BMW"), new NSString("Ford"), new NSString("Mazda") };
public ViewController(IntPtr handle) : base(handle)
{
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
// Do any additional setup after loading the view.
CollectionView = new NSCollectionView(this.View.Frame);
CollectionView.ItemPrototype = new ItemTemplate();
CollectionView.Content = Labels;
}
public override NSObject RepresentedObject
{
get
{
return base.RepresentedObject;
}
set
{
base.RepresentedObject = value;
// Update the view, if already loaded.
}
}
}
}
using System;
using AppKit;
using CoreGraphics;
using Foundation;
namespace Sandbox
{
public class ItemTemplate : NSCollectionViewItem
{
public ItemTemplate()
{
}
public override void LoadView()
{
base.LoadView();
this.View = new ItemView(new CGRect(0, 0, 0, 0));
}
public override NSObject RepresentedObject
{
get
{
return base.RepresentedObject;
}
set
{
base.RepresentedObject = value;
if (this.View != null)
{
((ItemView)this.View).Button.Title = value as NSString;
}
}
}
}
}
using System;
using AppKit;
using CoreGraphics;
namespace Sandbox
{
public class ItemView: NSView
{
public NSButton Button;
public ItemView(CGRect frameRect) : base(new CGRect(ViewController.Origin, ViewController.ItemSize))
{
Button = new NSButton(new CGRect(ViewController.Origin, ViewController.ButtonSize));
this.AddSubview(Button);
}
}
}
However, I keep getting an error in this part of the code:
public override void LoadView()
{
base.LoadView(); //<< ERROR happens here...
this.View = new ItemView(new CGRect(0, 0, 0, 0));
}
It looks like the code looks for a xib file to load (but I'm unsure whether the Xamarin framework may be calling the wrong API or whether NSCollectionViewItem cannot be used without a xib or something else is wrong with the code here altogether...)
Foundation.ObjCException
NSInternalInconsistencyException: -[NSNib _initWithNibNamed:bundle:options:] could not load the nibName: Sandbox_ItemTemplate in bundle (null).
error/stacktrace
at (wrapper managed-to-native) ObjCRuntime.Messaging.void_objc_msgSendSuper(intptr,intptr)
at AppKit.NSViewController.LoadView () [0x00027] in /Library/Frameworks/Xamarin.Mac.framework/Versions/7.2.0.3/src/Xamarin.Mac/AppKit/NSViewController.g.cs:872
at Sandbox.ItemTemplate.LoadView () [0x00001] in /Users/.../Projects/Xamarin_Mac___MacCollectionNew/Sandbox/ItemTemplate.cs:16
at (wrapper managed-to-native) ObjCRuntime.Messaging.void_objc_msgSend_IntPtr(intptr,intptr,intptr)
at AppKit.NSCollectionView.set_Content (Foundation.NSObject[] value) [0x0002c] in /Library/Frameworks/Xamarin.Mac.framework/Versions/7.2.0.3/src/Xamarin.Mac/AppKit/NSCollectionView.g.cs:1370
at Sandbox.ViewController.ViewDidLoad () [0x0002f] in /Users/.../Projects/Xamarin_Mac___MacCollectionNew/Sandbox/ViewController.cs:29
at (wrapper managed-to-native) AppKit.NSApplication.NSApplicationMain(int,string[])
at AppKit.NSApplication.Main (System.String[] args) [0x00040] in /Library/Frameworks/Xamarin.Mac.framework/Versions/7.2.0.3/src/Xamarin.Mac/AppKit/NSApplication.cs:109
at Sandbox.MainClass.Main (System.String[] args) [0x00007] in /Users/.../Projects/Xamarin_Mac___MacCollectionNew/Sandbox/Main.cs:10
Any guidance would be appreciated.
question from:
https://stackoverflow.com/questions/65854539/xamarin-mac-how-to-create-and-use-an-nscollectionview-with-100-code-no-xib