C# 如何在xamarin.studio的onclick事件中调用函数
我是一名C#开发人员,在xamarin.studio中为android开发一个简单的计算器。我的代码是C# 如何在xamarin.studio的onclick事件中调用函数,c#,android,xamarin-studio,C#,Android,Xamarin Studio,我是一名C#开发人员,在xamarin.studio中为android开发一个简单的计算器。我的代码是 btn9.Click += delegate { Buttonclick(); }; btnadd.Click += delegate { operationbuttonclick (); }; btnsub.Click += delegate {
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;
};
我在buttonclick()中出错;和操作按钮单击();上述代码的第2行和第4行。
我的ButtonClick方法和操作ButtonClick方法是
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);
}
private void按钮单击(对象发送方,事件参数e)
{
EditText txt2=FindViewById(Resource.Id.textView1);
EditText txt1=findviewbyd(Resource.Id.textView2);
按钮sbutton=(发送器为按钮);
双倍oldno、newno、buttonno;
如果(应清除)
{
txt1.Text=“”;
oldno=0;
shouldclear=false;
}
其他的
{
oldno=double.Parse(txt1.Text);
hasdecim=true;
}
buttonno=double.Parse(sbutton.Text);
新编号=(旧编号*10)+按钮编号;
如果(isfirst)
{
num1=新编号;
}
其他的
{
num2=newno;
}
txt1.Text+=sbutton.Text;
计算(符号);
}
及
private void operation按钮单击(对象发送方,事件参数e)
{
EditText txt2=FindViewById(Resource.Id.textView1);
EditText txt1=findviewbyd(Resource.Id.textView2);
num1=结果;
按钮sourcebutton=(发送方为按钮);
字符串运算符Symbol=sourcebutton.Text;
如果(isfirst)
isfirst=false;
hasdecim=true;
符号=运算符符号;
Text=result.ToString();
}
在VisualStudio中,可以通过转到button的事件处理程序并在onclick event中设置方法在xamarin中如何执行来轻松完成。我在谷歌上搜索过这个问题,找不到合适的解决方案。任何帮助都将不胜感激。您可以使用事件处理程序代替委托,请尝试以下操作
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;
};
并且代码保持不变。你可能会喜欢打字,但这里有一个简短的版本。没什么新的,它是从.NET2.0iirc开始工作的
btn9.单击+=按钮单击;
b添加。单击+=操作按钮单击;
单击+=操作按钮单击;
btndiv.单击+=操作按钮单击;
点击+=操作按钮点击;
点击+=(o,e)=>{
txt1.Text=result.ToString();
isfirst=true;
hasdecim=true;
shouldclear=true;
};
点击+=(o,e)=>{
txt1.Text=“0”;
结果=0;
isfirst=true;
shouldclear=true;
hasdecim=假;
};
能否将参数传递给操作按钮单击?
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;
};