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

时间:2015-01-20 14:35:29

标签: c# mono xamarin monomac

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

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

  • 我应该在mainWindowController中添加NSApplicationDelegate中的按钮吗?
  • 我是否在“Window.cs”文件中将其连线?

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

提前致谢, 伊夫

1 个答案:

答案 0 :(得分:3)

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

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


创建AppDelegate

一些介绍

显示窗口

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

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

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

    创建按钮

    方法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);
    4. 将点击事件连接到按钮

      此部分再次位于方法 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;
              }
          }
      }
      

      程序类(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();
              }
          }
      }
      

      欢迎所有评论和建议。