How to call a function in onclick event in xamarin.studio

I am a C # developer working on a simple calculator in xamarin.studio for Android, my code is

btn9.Click += delegate { Buttonclick(); }; btnadd.Click += delegate { operationbuttonclick (); }; btnsub.Click += delegate { operationbuttonclick (); }; btndiv.Click += delegate { operationbuttonclick (); }; btnmul.Click += delegate { operationbuttonclick (); }; btneql.Click += delegate { txt1.Text=result.ToString(); isfirst=true; hasdecimal=true; shouldclear=true; }; btnCE.Click += delegate { txt1.Text="0"; result=0; isfirst=true; shouldclear=true; hasdecimal=false; }; 

I get an error in buttonclick (); and operationbuttonclick (); line 2 and 4 of the above code. my buttonclickmethod and operationbuttonclick methods

 private void Buttonclick(object sender,EventArgs e) { EditText txt2 = FindViewById<EditText> (Resource.Id.textView1); EditText txt1 = FindViewById<EditText> (Resource.Id.textView2); Button sbutton = (sender as Button); double oldno, newno,buttonno; if(shouldclear) { txt1.Text=""; oldno=0; shouldclear=false; } else { oldno=double.Parse(txt1.Text); hasdecimal = true; } buttonno=double.Parse(sbutton.Text); newno = (oldno * 10) + buttonno; if(isfirst) { num1=newno; } else { num2=newno; } txt1.Text += sbutton.Text; Calculate (symbol); } 

and

 private void operationbuttonclick(object sender,EventArgs e) { EditText txt2 = FindViewById<EditText> (Resource.Id.textView1); EditText txt1 = FindViewById<EditText> (Resource.Id.textView2); num1 = result; Button sourcebutton=(sender as Button); string operatorsymbol = sourcebutton.Text; if (isfirst) isfirst = false; hasdecimal = true; symbol = operatorsymbol; txt1.Text = result.ToString (); } 

In the visual studio, this can be easily done by going to the button event handler and setting the method in the onclick event, how to do it in xamarin. I searched this for a search and did not find a suitable solution. Any help would be appreciated.

+8
android c # xamarin-studio
source share
2 answers

You can use event handler instead of delegates, try

 btn9.Click += (object sender, EventArgs e) => { Buttonclick (sender, e); }; btnadd.Click += (object sender, EventArgs e) => { operationbuttonclick (sender, e); }; btnmul.Click += (object sender, EventArgs e) => { operationbuttonclick (sender, e); }; btnsub.Click += (object sender, EventArgs e) => { operationbuttonclick (sender, e); }; btndiv.Click += (object sender, EventArgs e) => { operationbuttonclick (sender, e); }; btneql.Click += delegate { txt1.Text=result.ToString(); isfirst=true; hasdecimal=true; shouldclear=true; }; btnCE.Click += delegate { txt1.Text="0"; result=0; isfirst=true; shouldclear=true; hasdecimal=false; }; 

and the remaining code.

+7
source share

You may like to print a lot, but here is a short version. Nothing new, it works with .NET2.0 iirc

 btn9.Click += Buttonclick; btnadd.Click += operationbuttonclick; btnsub.Click += operationbuttonclick; btndiv.Click += operationbuttonclick; btnmul.Click += operationbuttonclick; btneql.Click += (o,e) => { txt1.Text=result.ToString(); isfirst=true; hasdecimal=true; shouldclear=true; }; btnCE.Click += (o,e) => { txt1.Text="0"; result=0; isfirst=true; shouldclear=true; hasdecimal=false; }; 
+6
source share

All Articles