在 C# MonoMac 中动态创建控件/对象(NSButton、NSLabel)

Dynamically create controls / objects in C# MonoMac (NSButton, NSLabel)

我正在尝试在代码中创建一个 NSButton 和一个 NSLabel。我能够创建它,但我不知道我应该将它添加到哪里,然后连接它以进行连接。

我不想使用 XCode(带 Interface Builder),因为这些控件需要动态实例化。

有人可以为我指明正确的方向,以便在 MonoMac/C# 中动态创建和连接 对象吗?

提前致谢, 伊夫

在 Apple 文档和 Xamarin 文档中进行研究后,我想到了以下解决方案。我不知道它是否适用于更复杂的场景,但这是一个开始。

我不知道这是否是最佳做法。无论哪种方式,我希望这也能帮助其他人。如果我找到更多信息,我会更新这个。


创建 AppDelegate

一些介绍

正在显示window

AppDelegate class 将创建 window 和其他 NSObject(例如 NSButton 等)。 .)

首先覆盖方法 FinishedLaunching - 在该方法中我们创建 window (NSWindow) - 见行var window = new NSWindow(...)。之后有一些样板

  1. 使 window 从左上角开始显示 window.CascadeTopLeftFromPoint (new PointF (20, 20));
  2. "Show" window 使用 window.MakeKeyAndOrderFront (null);

创建按钮

方法CreateCloseButton() 将生成一个带有标题、外观等的按钮。已设置。有关选择 BezelStyle 的简单指南,请查看此 link:[ http://psionides.eu/2014/10/06/a-guide-to-nsbutton-styles/ ].

将按钮添加到 window

此部分再次位于方法 FinishedLaunching 内。采取的步骤:

  1. 为按钮的 "subview" 制作一个矩形(或 RectangleF)。这意味着在按钮所在的位置创建一个矩形。如果你把它做得太小,按钮将不会完全显示,但它会有一些缺失的边缘。代码是 var closeButtonRect = new RectangleF (8, 8, closeButtonSize.Width, closeButtonSize.Height);.
  2. 接下来我们必须让我们的按钮知道这个矩形是它将居住的地方,方法是说:closeButton.Frame = closeButtonRect;
  3. 然后我们将按钮(或 NSView)添加到我们的 window。 window.ContentView.AddSubview (closeButton);.

将点击事件连接到按钮

此部分再次位于方法 FinishedLaunching 内。但它可以很容易地在别处设置。基本上这作为 c# 事件(见下面的片段)。

// Attach an event to the button. When "Activated" / "Clicked" it will close the application.
                closeButton.Activated += (object sender, EventArgs e) => { 
                    NSApplication.SharedApplication.Terminate(closeButton);
                };

完整代码

AppDelegate Class

using System;
using System.Drawing;
using MonoMac.Foundation;
using MonoMac.AppKit;

namespace MonoMacTest02
{
    public class AppDelegate : NSApplicationDelegate
    {
        public override void FinishedLaunching (MonoMac.Foundation.NSObject notification)
        {
            // Get the button
            var closeButton = CreateCloseButton ();
            // Get size and create rectangle for the view                 
            var closeButtonSize = closeButton.Frame.Size;
            var closeButtonRect = new RectangleF (8, 8, closeButtonSize.Width, closeButtonSize.Height);
            // Apply the rectangle to the frame
            closeButton.Frame = closeButtonRect;

            // Attach an event to the button. When "Activated" / "Clicked" it will close the application.
            closeButton.Activated += (object sender, EventArgs e) => { 
                NSApplication.SharedApplication.Terminate(closeButton);
            };

            // Creating the NSWindow object
            var window = new NSWindow (
                new RectangleF(0, 0, 400, 300), // Sets the size of the window
                NSWindowStyle.Titled, // Style of the window
                NSBackingStore.Buffered,
                false
            ) {
                // Adding a title
                Title = "MonoMacTest02 (Adding a button)"
            };

            // Add our button to the window
            // AddSubView expects an NSView object. All UI controls are derived from NSView, so we can add the 'closeButton' itself.
            window.ContentView.AddSubview (closeButton);

            window.CascadeTopLeftFromPoint (new PointF (20, 20));
            window.MakeKeyAndOrderFront (null);
        }

        // Creating the button
        private NSButton CreateCloseButton() {
            var closeButton = new NSButton ();
            closeButton.Title = "Close";
            closeButton.BezelStyle = NSBezelStyle.Rounded;

            closeButton.SizeToFit ();

            return closeButton;
        }
    }
}

程序 Class (static void Main())

using System;
using MonoMac.AppKit;

namespace MonoMacTest02 {
    public class Program {
        static void Main(string[] args) {
            NSApplication.Init();

            var application = NSApplication.SharedApplication;
            application.Delegate = new AppDelegate();
            application.Run();
        }
    }
}

欢迎大家提出意见和建议。