在 Apple 文档和 Xamarin 文档中进行研究,我想出了以下解决方案。我不知道它是否适用于更复杂的场景,但这是一个开始。
我不知道这是否是最佳做法。无论哪种方式,我希望这对其他人也有帮助。如果我找到更多信息,我会更新。
创建 AppDelegate
一些介绍
显示窗口
AppDelegate 类将创建窗口和其他 NSObject(如 NSButton 等)
首先覆盖方法 FinishedLaunching - 在该方法中我们创建窗口 (NSWindow) - 参见 var window = new NSWindow(...) 处的行。之后有一些样板
- 使窗口从左上角开始显示
window.CascadeTopLeftFromPoint (new PointF (20, 20));
- 使用
window.MakeKeyAndOrderFront (null);“显示”窗口
创建按钮
CreateCloseButton() 方法将生成一个带有标题、外观等已经设置好的按钮。有关选择 BezelStyle 的简单指南,请查看此链接:[http://psionides.eu/2014/10/06/a-guide-to-nsbutton-styles/]。
将按钮添加到窗口
此部分再次位于方法 FinishedLaunching 中。采取的步骤:
- 为按钮的“子视图”制作一个矩形(或
RectangleF)。这意味着在按钮所在的位置创建一个矩形。如果你把它做得太小,按钮将不会完全显示,但它会有一些缺失的边缘,例如。代码是var closeButtonRect = new RectangleF (8, 8, closeButtonSize.Width, closeButtonSize.Height);。
- 接下来,我们必须通过说:
closeButton.Frame = closeButtonRect; 来让我们的按钮知道这个矩形就是它所在的区域。
- 然后我们将按钮(或 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();
}
}
}
欢迎所有意见和建议。