在我的项目中,我有一个带有控制器的视图,称为 TaskOverviewView,只有一个 NSTextField,我想像小部件一样使用它。在 NSWindow 中,我也有 NSCustomView 和 NSScrollView。在展位上,我尝试使用 addSubview(NSView) 方法将 TaskOverviewViewController 的视图实例添加为子视图。虽然它适用于 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);
}
}
}