Yve*_*lpe 5 c# mono monomac xamarin
我正在尝试在代码中创建NSButton和NSLabel.我能够创建它,但我不知道应该在哪里添加它然后连接它以进行连接.
我不想使用XCode(w/Interface Builder),因为这些控件需要动态实例化.
有人能指出我在MonoMac/C#中动态创建和连接对象的正确方向吗?
谢谢,Yves
在 Apple 文档和 Xamarin 文档中进行研究后,我提出了以下解决方案。我不知道它是否适用于更复杂的场景,但这是一个开始。
我不知道这是否是最佳实践。不管怎样,我希望这也能帮助其他人。如果我找到更多信息,我会更新。
显示窗口
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;。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)
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)
欢迎所有意见和建议。
| 归档时间: |
|
| 查看次数: |
936 次 |
| 最近记录: |