【发布时间】:2021-08-24 03:01:17
【问题描述】:
我想从 NSCollectionViewItem 内部获取对主应用程序视图控制器的引用。
我创建了这个函数:
public partial class MyViewItemController : NSCollectionViewItem {
private ViewController controller;
// Called when created from unmanaged code
public MyViewItemController(IntPtr handle) : base(handle) {
Initialize();
}
partial void buttonAction(Foundation.NSObject sender) {
var button = sender as NSButton;
controller.performSomething(model);
}
private ViewController getController() {
var localDelegate = CollectionView.Delegate as CollectionViewDelegate;
var controller = localDelegate.ParentViewController as ViewController;
return controller;
}
}
但是如果我在 Initialize 方法中调用它会引发错误:
private ViewController controller;
// Shared initialization code
void Initialize() {
controller = getController();// Failed to lookup the required marshalling information.
}
从错误看来,应用程序生命周期中的工作还为时过早。
我是否可以通过其他方式在 View Item 类中获取视图控制器,或者我可以覆盖的任何其他方法,而不是可以设置控制器变量的 Initialize()?
【问题讨论】:
-
在主应用程序视图控制器中实现按钮操作并将按钮连接到第一响应者。
标签: swift objective-c macos xamarin nscollectionview