-1

在我的项目中,我有一个带有控制器的视图,称为 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);
        }
    }
}
4

1 回答 1

0

该问题与 ScrollView 和 CustomView 无关。相反,它与调用顺序相关。如果我改为切换它,则 ScrollView 将不起作用,但 CustomView. 解决方案与一个问题相关,我在这里问:NullReferenceException when access GUI Components in Controllers constructor

与其将代码放入Initialize()方法中,不如将其放入AwakeFromNib()方法中。

于 2012-10-24T12:56:01.587 回答