如何在xamarin.studio中的onclick事件中调用函数

 请叫我姚灬小贱_______ 发布于 2023-02-13 20:15

我是C#开发人员,在xamarin.studio中使用简单的计算器,我的代码是

        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()中遇到错误; 和operationbuttonclick(); 上面代码的第2行和第4行.我的buttonclick方法和operationbuttonclick方法是

private void Buttonclick(object sender,EventArgs e)
    {
        EditText txt2 = FindViewById (Resource.Id.textView1);
        EditText txt1 = FindViewById (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 operationbuttonclick(object sender,EventArgs e)
    {
        EditText txt2 = FindViewById (Resource.Id.textView1);
        EditText txt1 = FindViewById (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 ();
    }

在visual studio中,可以通过按钮的事件处理程序轻松完成,并在onclick事件中设置方法如何在xamarin中执行.我已经google了这个,找不到任何合适的解决方案.任何帮助将不胜感激.

2 个回答
  • 你可能喜欢打字很多,但这里是短版.没有什么新东西,它可以用于.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;
    };
    

    2023-02-13 20:18 回答
  • 您可以使用事件处理程序代替委托,试试这个

    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;
            };
    

    和剩下的代码相同.

    2023-02-13 20:21 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有