【问题标题】:Dynamically create controls / objects in C# MonoMac (NSButton, NSLabel)在 C# MonoMac (NSButton, NSLabel) 中动态创建控件/对象
【发布时间】:2015-03-18 20:01:06
【问题描述】:

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

我不想使用 XCode(带界面生成器),因为这些控件需要动态实例化。

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

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

提前致谢, 伊夫

【问题讨论】:

    标签: c# mono xamarin monomac


    【解决方案1】:

    在 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# 事件工作(见下面的 sn-p)。

    // 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 类

    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;
            }
        }
    }
    

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

    欢迎所有意见和建议。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-16
      • 1970-01-01
      • 1970-01-01
      • 2019-10-21
      相关资源
      最近更新 更多