如何使用xamarin以编程方式制作按钮?

par*_*r88 3 c# user-interface ios xamarin

我正在尝试使用xamarin ide(c#)以编程方式创建按钮.我需要用什么代码来创建按钮,设置其大小,设置文本,设置背景颜色以及设置约束?有没有办法将按钮定义为屏幕宽度的1/4?提前致谢.

iam*_*rus 14

首先创建按钮

UIButton button = new UIButton(); 
CGRect ScreenBounds = UIScreen.MainScreen.Bounds;
float buttonWidth = (float)ScreenBounds.X / 4;
button.Frame = new CGRect (0f, 0f, buttonWidth, 50f); 
button.SetTitle ("Title", UIControlState.Normal);
button.BackgroundColor = UIColor.Green;
Run Code Online (Sandbox Code Playgroud)

然后将其作为子视图添加到活动视图中

this.AddSubview (button);
Run Code Online (Sandbox Code Playgroud)

并添加事件 TouchUpInside

button.TouchUpInside += (object sender, System.EventArgs e) => {
   Debug.WriteLine( "Button Clicked!");
};
Run Code Online (Sandbox Code Playgroud)