在我的项目中,我有一个带控制器的View,名为TaskOverviewView,只有一个NSTextField,我想像小工具一样使用它。在NSWindow,我有一个NSCustomView和NSScrollView。在展位上,我尝试使用addSubview(NSView)方法将TaskOverviewViewController视图的实例添加为Subview。虽然它适用于ScrollView,但是在CustomView上看不到窗口小部件。
代码:
TaskOverviewViewController的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using MonoMac.Foundation;
using MonoMac.AppKit;
namespace GetTheMilk.MacGUI
{
public partial class TaskOverviewViewController : MonoMac.AppKit.NSViewController
{
#region Constructors
// Called when created from unmanaged code
public TaskOverviewViewController (IntPtr handle) : base (handle)
{
Initialize ();
}
// Called when created directly from a XIB file
[Export ("initWithCoder:")]
public TaskOverviewViewController (NSCoder coder) : base (coder)
{
Initialize ();
}
// Call to load from the XIB/NIB file
public TaskOverviewViewController () : base ("TaskOverviewView", NSBundle.MainBundle)
{
Initialize ();
}
// Shared initialization code
void Initialize ()
{
_taskName = new NSTextField();
_taskName.StringValue = "HUHUH";
}
#endregion
//strongly typed view accessor
public new TaskOverviewView View {
get {
return (TaskOverviewView)base.View;
}
}
}
}
我的StartupWindow的控制器:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using MonoMac.Foundation;
using MonoMac.AppKit;
namespace GetTheMilk.MacGUI
{
public partial class StartupWindowController : MonoMac.AppKit.NSWindowController
{
#region Constructors
// Called when created from unmanaged code
public StartupWindowController (IntPtr handle) : base (handle)
{
Initialize ();
}
// Called when created directly from a XIB file
[Export ("initWithCoder:")]
public StartupWindowController (NSCoder coder) : base (coder)
{
Initialize ();
}
// Call to load from the XIB/NIB file
public StartupWindowController () : base ("StartupWindow")
{
Initialize ();
}
// Shared initialization code
void Initialize ()
{
}
#endregion
//strongly typed window accessor
public new StartupWindow Window {
get {
return (StartupWindow)base.Window;
}
}
partial void _ButtonClick(NSObject sender)
{
_Label1.StringValue = "Hello";
TaskOverviewViewController widget = new TaskOverviewViewController();
_cview.AddSubview(widget.View);
_scrillView.AddSubview(widget.View);
}
}
}
答案 0 :(得分:0)
问题与ScrollView和CustomView无关。相反,它与呼叫顺序相关。如果我切换它而不是ScrollView不能工作,而是CustomView。解决方案与问题相关,我在这里问:NullReferenceException when accessing GUI Components in Controllers constructor
不应将代码放入Initialize()
方法,而应将其放入AwakeFromNib()
方法。