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

Yve*_*lpe 5 c# mono monomac xamarin

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

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

  • 我应该在mainWindowController中添加NSApplicationDelegate中的按钮吗?
  • 我是否在"Window.cs"文件中将其连接起来?

有人能指出我在MonoMac/C#中动态创建和连接对象的正确方向吗?

谢谢,Yves

Yve*_*lpe 3

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

我不知道这是否是最佳实践。不管怎样,我希望这也能帮助其他人。如果我找到更多信息,我会更新。


创建 AppDelegate

一些介绍

显示窗口

AppDelegate类将创建窗口和其他NSObject 例如 NSButton 等..)

首先覆盖方法FinishedLaunching - 在该方法中我们创建窗口 ( NSWindow ) - 请参阅 处的行var window = new NSWindow(...)。之后还有一些样板

  1. 让窗口从左上角开始显示window.CascadeTopLeftFromPoint (new PointF (20, 20));
  2. 使用“显示”窗口window.MakeKeyAndOrderFront (null);

创建按钮

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

将按钮添加到窗口

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

  1. RectangleF为按钮的“子视图”创建一个矩形(或)。这意味着创建一个按钮所在的矩形。如果将其设置得太小,按钮将不会完全显示,但会缺少一些边缘。代码是var closeButtonRect = new RectangleF (8, 8, closeButtonSize.Width, closeButtonSize.Height);.
  2. 接下来,我们必须通过说: 来让我们的按钮知道它将居住在这个矩形中closeButton.Frame = closeButtonRect;
  3. 然后我们将按钮(或 NSView)添加到我们的窗口中。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);
                };
Run Code Online (Sandbox Code Playgroud)

完整代码

应用程序委托类

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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

程序类(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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

欢迎所有意见和建议。