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

C# 如何使用xamarin以编程方式制作按钮?,c#,ios,user-interface,xamarin,C#,Ios,User Interface,Xamarin,我正在尝试使用xamarin ide(c#)以编程方式创建按钮。创建按钮、设置其大小、设置其文本、设置其背景色和设置其约束需要什么代码?有没有办法将按钮定义为屏幕宽度的1/4?提前谢谢 首先创建按钮 UIButton button = new UIButton(); CGRect ScreenBounds = UIScreen.MainScreen.Bounds; float buttonWidth = (float)ScreenBounds.X / 4; button.Frame = new

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

首先创建按钮

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;
然后将其作为子视图添加到活动视图中

this.AddSubview (button);
并为
TouchUpInside添加事件

button.TouchUpInside += (object sender, System.EventArgs e) => {
   Debug.WriteLine( "Button Clicked!");
};
试试这个:

public class CalendarButton: UIButton
{

    public int ID;
    public int State;
    public CalendarButton (string text, int id)
    {
        ID = id;
        SetTitle (text, UIControlState.Normal);
    }
    public CalendarButton ():base(UIButtonType.System){
        State = 0;
    }
    public CalendarButton(IntPtr handle) : base(handle) { }
}
此自定义视图:

public class PickerView: UIView
{

int _numberItemPerRow = 7;
int _numberRows = 6;
nfloat _itemSize = 40;

UIColor _labelColor;
UIColor _oneTouchColor;
UIColor _twoTouchColor;
List<CalendarButton> _buttons;

public PickerView (UIColor lb, UIColor one, UIColor two)
{
    _labelColor = lb;
    _oneTouchColor = one;
    _twoTouchColor = two;
}
public PickerView(IntPtr handle) : base(handle) {

}


public override void Draw (CoreGraphics.CGRect rect)
{
    var height = CalculateHeight (rect.Width);
    this.Frame = new CoreGraphics.CGRect (rect.X, rect.Y, rect.Width, height);
    InitLayout ();
}

public override void SetNeedsLayout ()
{
    base.SetNeedsLayout ();
}

/*
 * Internal functions
 */

nfloat CalculateHeight(nfloat width)
{
    _itemSize = width / 7;
    return (nfloat)(_itemSize * (_numberRows + 1) + 70);
}

void InitLayout ()
{
    DateTime currentTime = DateTime.Now;
    // add date label - first row
    for (int i = 0; i < 7; i++) {
        UILabel lb = new UILabel ();
        lb.Frame = new CoreGraphics.CGRect (i * _itemSize, 0, _itemSize, _itemSize);
        lb.Text = Constants.DaysInWeek.ElementAt (i);
        lb.TextAlignment = UITextAlignment.Center;
        lb.ClipsToBounds = true;
        lb.Layer.CornerRadius = _itemSize / 2;
        lb.BackgroundColor = _labelColor;
        lb.Layer.BorderWidth = 1;
        lb.Layer.BorderColor = UIColor.Black.CGColor;
        this.AddSubview (lb);
    }
    // add month
    UILabel lbMonth = new UILabel();
    lbMonth.Frame = new CoreGraphics.CGRect (0, _itemSize, this.Bounds.Width, 70);
    lbMonth.TextAlignment = UITextAlignment.Center;
    lbMonth.TextColor = UIColor.Black;
    lbMonth.Font = UIFont.BoldSystemFontOfSize (22);
    Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
    string[] monthNames = 
        System.Globalization.CultureInfo.CurrentCulture
            .DateTimeFormat.MonthGenitiveNames;

    lbMonth.Text = string.Format ("{0} {1}", monthNames[currentTime.Month], currentTime.Year);
    this.AddSubview (lbMonth);
    // draw the table
    UIView viewTable = new UIView();
    viewTable.BackgroundColor = UIColor.Clear;
    viewTable.Frame = new CoreGraphics.CGRect (0, _itemSize + 70, this.Bounds.Width, _itemSize * _numberRows);
    _buttons = new List<CalendarButton> ();

    for (int row = 0; row < _numberRows; row++) {
        for (int col = 0; col < _numberItemPerRow; col++) {
            CalendarButton btn = new CalendarButton ();
            btn.Frame = new CoreGraphics.CGRect (col * _itemSize, row * _itemSize, _itemSize, _itemSize);
            btn.BackgroundColor = UIColor.White; // default color;
            btn.Layer.BorderColor = UIColor.Black.CGColor;
            btn.Layer.BorderWidth = 1;
            btn.Layer.CornerRadius = _itemSize / 2;
            btn.Tag = 0;

            btn.SetTitleColor (UIColor.Black, UIControlState.Normal);
            _buttons.Add (btn);
            viewTable.AddSubview (btn);
        }
    }
    // get the first and last date of current month;
    DateTime nexMonth = currentTime.AddMonths(1);
    var startDate = new DateTime(nexMonth.Year, nexMonth.Month, 1);
    var endDate = startDate.AddMonths(1).AddDays(-1);
    int currentDateOfWeek = (int)startDate.Date.DayOfWeek;
    for (int i = 0; i < endDate.Day; i++) {
        CalendarButton btn = _buttons.ElementAt (currentDateOfWeek + i);
        btn.BackgroundColor = _labelColor;
        btn.SetTitle ((i + 1).ToString (), UIControlState.Normal);
        btn.ID = i + 1;
        btn.TouchUpInside += (object sender, EventArgs e) => {
            CalendarButton sd = (CalendarButton)sender;
            sd.Tag++;
            if (sd.Tag%3 == 0) {
                // gray
                sd.State = 0;
                sd.BackgroundColor = _labelColor;
            }else if(sd.Tag%3 == 1){
                // red
                sd.State = 1;
                sd.BackgroundColor = _oneTouchColor;
            }else{
                // green
                sd.State = 2;
                sd.BackgroundColor = _twoTouchColor;
            }
        };
    }
    this.AddSubview (viewTable);
}
}
公共类PickerView:UIView
{
int_numberItemPerRow=7;
int _numberRows=6;
nfloat_itemSize=40;
UIColor labelColor;
UIColor oneTouchColor;
UIColor twoTouchColor;
列表按钮;
公共PickerView(UIColor lb、UIColor one、UIColor II)
{
_labelColor=lb;
_一触式颜色=一;
_twoTouchColor=2;
}
公共PickerView(IntPtr句柄):基本(句柄){
}
公共覆盖无效绘制(CoreGraphics.CGRect)
{
变量高度=计算高度(矩形宽度);
this.Frame=new CoreGraphics.CGRect(rect.X,rect.Y,rect.Width,height);
InitLayout();
}
公共覆盖无效SetNeedsLayout()
{
base.SetNeedsLayout();
}
/*
*内部功能
*/
nfloat计算高度(nfloat宽度)
{
_itemSize=宽度/7;
返回(nfloat)(_itemSize*(_numberRows+1)+70);
}
void InitLayout()
{
DateTime currentTime=DateTime.Now;
//添加日期标签-第一行
对于(int i=0;i<7;i++){
UILabel lb=新的UILabel();
lb.Frame=new CoreGraphics.CGRect(i*\u itemSize,0,\u itemSize,\u itemSize);
lb.Text=Constants.DaysInWeek.ElementAt(i);
lb.TextAlignment=UITextAlignment.Center;
lb.clipstobunds=true;
lb.Layer.CornerRadius=_itemSize/2;
lb.BackgroundColor=_labelColor;
lb.Layer.BorderWidth=1;
lb.Layer.BorderColor=UIColor.Black.CGColor;
此.AddSubview(lb);
}
//添加月份
UILabel lbMonth=新UILabel();
lbMonth.Frame=new CoreGraphics.CGRect(0,_itemSize,this.Bounds.Width,70);
lbMonth.TextAlignment=UITextAlignment.Center;
lbMonth.TextColor=UIColor.Black;
lbMonth.Font=UIFont.BoldSystemFontOfSize(22);
Thread.CurrentThread.CurrentCulture=新文化信息(“en-US”);
字符串[]月份=
System.Globalization.CultureInfo.CurrentCulture
.DateTimeFormat.MonthGenitiveNames;
lbMonth.Text=string.Format(“{0}{1}”,monthNames[currentTime.Month],currentTime.Year);
this.AddSubview(lbMonth);
//画桌子
UIView viewTable=新UIView();
viewTable.BackgroundColor=UIColor.Clear;
viewTable.Frame=new CoreGraphics.cgrit(0,_itemSize+70,this.Bounds.Width,_itemSize*_numberRows);
_按钮=新列表();
对于(int row=0;row<\u numberRows;row++){
for(int col=0;col<\u numberItemPerRow;col++){
CalendarButton btn=新的CalendarButton();
btn.Frame=new CoreGraphics.cgrct(列*\u itemSize,行*\u itemSize,\u itemSize,\u itemSize);
btn.BackgroundColor=UIColor.White;//默认颜色;
btn.Layer.BorderColor=UIColor.Black.CGColor;
btn.Layer.BorderWidth=1;
btn.Layer.CornerRadius=\u itemSize/2;
btn.Tag=0;
btn.SetTitleColor(UIColor.Black,UIControlState.Normal);
_按钮。添加(btn);
viewTable.AddSubview(btn);
}
}
//获取当月的第一个和最后一个日期;
DateTime nexMonth=currentTime.AddMonths(1);
var startDate=新日期时间(nexMonth.Year,nexMonth.Month,1);
var endDate=开始日期。添加月份(1)。添加天数(-1);
int currentDateOfWeek=(int)startDate.Date.DayOfWeek;
for(int i=0;i{
CalendarButton sd=(CalendarButton)发送者;
sd.Tag++;
如果(sd.标记%3==0){
//灰色的
sd.State=0;
sd.BackgroundColor=_labelColor;
}else if(sd.标记%3==1){
//红色的
sd.State=1;
sd.BackgroundColor=_oneTouchColor;
}否则{
//绿色的
sd.State=2;
sd.BackgroundColor=\u twoTouchColor;
}
};
}
this.AddSubview(viewTable);
}
}
结果:


什么站台?iOS、Android、Forms等都忘了提到:/您可能还希望在ViewController的“ViewDidLoad”方法中执行此操作。