Xamarin Mac - 如何使用 100% 代码无 xib创建和使用 NSCollectionView?

问题描述

使用这些 SO 答案中的信息:

我想出了以下几点:

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));
        }

        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);
        }
    }
}

但是,我在这部分代码中不断出现错误:

        public override void LoadView()
        {
            base.LoadView(); //<< ERROR happens here...

            this.View = new ItemView(new CGRect(0,0));
        }

看起来代码寻找要加载的 xib 文件(但我不确定 Xamarin 框架是否可能调用了错误的 API,或者 NSCollectionViewItem 是否在没有 xib 的情况下无法使用,或者这里的代码完全有问题...)

Foundation.ObjCException NSInternalInconsistencyException: -[NSNib _initWithNibNamed:bundle:options:] 无法加载包中的 nibName: Sandbox_ItemTemplate (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 

任何指导将不胜感激。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)