Develop Android apps with C # + Xamarin-Add controls dynamically

Source: Internet
Author: User

public class Myorders:activity {private DynamicControl _dynacontrol;        Public Myorders () {_dynacontrol = new DynamicControl (); } protected override void OnCreate (bundle bundle) {base.            OnCreate (bundle);                try {setcontentview (Resource.Layout.MyOrders);                var layout = (tablelayout) Findviewbyid (Resource.Id.myorders_layout);                var header = AddHeader (); Layout.                AddView (header); Layout.                AddView (Addpendingserviceitem ("Service 1")); Layout.                AddView (Addpendingserviceitem ("Service 2")); Layout.                AddView (Addpendingserviceitem ("Service 3")); Layout.                AddView (Addconfirmedserviceitem ("Service 4")); Layout.                AddView (Addconfirmedserviceitem ("Service 5")); Layout.                AddView (Addconfirmedserviceitem ("Service 6")); Layout.    AddView (Addconfirmedserviceitem ("Service 7"));        } catch (Exception ex) {this. Showalert (ex.            Message);            }} public View AddHeader () {var header = new TableRow (this); Header. Layoutparameters = new Tablelayout.layoutparams (TableLayout.LayoutParams.FillParent, TABLELAYOUT.LAYOUTPARAMS.W            Rapcontent); Header.            AddView (_dynacontrol.newtext (This, "Service")); Header.            AddView (_dynacontrol.newtext (This, "Status")); Header.            AddView (_dynacontrol.newtext (This, "Created at")); Header.            AddView (_dynacontrol.newtext (This, "Action"));        return header;            Public View Addpendingserviceitem (string serviceName) {var tableRow = new TableRow (this); Tablerow.layoutparameters = new Tablelayout.layoutparams (TableLayout.LayoutParams.FillParent, Table            Layout.LayoutParams.WrapContent); Tablerow.addview (_dynacontrol.newtext (this, string. Format("{0}", ServiceName)); Tablerow.addview (_dynacontrol.newtext (this, string.            Format ("{0}", "Pending"), color.orangered)); Tablerow.addview (_dynacontrol.newtext (this, string.            Format ("{0}", "2015-5-19 \r\n14:20:33"));            Tablerow.addview (_dynacontrol.addbutton (This, "Details"));        return tableRow;            Public View Addconfirmedserviceitem (string serviceName) {var tableRow = new TableRow (this); Tablerow.addview (_dynacontrol.newtext (this, string.            Format ("{0}", ServiceName)); Tablerow.addview (_dynacontrol.newtext (this, string.            Format ("{0}", "confirmed"), Color.darkgreen)); Tablerow.addview (_dynacontrol.newtext (this, string.            Format ("{0}", "2015-5-19 \ r \ n 14:30:43"));            Tablerow.addview (_dynacontrol.addbutton (This, "rebook")); Tablerow.layoutparameters = new Tablelayout.layoutparams (TableLayout.LayoutParams.FillParent, TABLELAYOUT.L Ayoutparams.wrapcontENT);        return tableRow; }    }


Auxiliary classes:

public class DynamicControl    {public        Button AddButton (context context, string text, EventHandler clickaction = Nu ll)        {            var btn = new Button (context);            Btn. SetText (text, TextView.BufferType.Normal);            if (clickaction! = null)            {                btn. Click + = clickaction;            }                        return btn;        }        Public TextView NewText (context context, string str, color? color = null)        {            var text = new TextView (context); 
   if (color. HasValue)            {                text. SetTextColor (color. Value);            }            Text. SetText (str, TextView.BufferType.Normal);            return text;        }    }


Develop Android apps with C # + Xamarin-Add controls dynamically

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.