2013-11-29 5 views
8

में ऑनक्लिक ईवेंट में एक समारोह कॉल करने के लिए मैं सी # डेवलपर एंड्रॉयड मेरी कोड के लिए xamarin.studio में एक साधारण कैलकुलेटर पर काम कर रहा हूँकैसे 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 में त्रुटि हो रही है() है; और ऑपरेशनबटन लिंक(); उपरोक्त कोड की रेखा 2 और 4। मेरी buttonclickmethod और operationbuttonclick तरीकों

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

दृश्य स्टूडियो में यह आसानी से onclick घटना में बटन के ईवेंट हैंडलर जा रहा है और विधि सेट कैसे Xamarin में यह करने के लिए किया जा सकता है कर रहे हैं। मैंने इसके लिए गुगल किया है और कोई उचित समाधान नहीं मिल रहा है। किसी भी तरह की सहायता को आभार समझेंगे।

उत्तर

7

आप प्रतिनिधियों के स्थान पर ईवेंट हैंडलर का उपयोग कर सकते, इस

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

और शेष कोड एक ही प्रयास करें।

6

आप बहुत टाइपिंग का आनंद ले सकते हैं, लेकिन यहां संक्षिप्त संस्करण है। कुछ नया नहीं, यह .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; 
}; 
+0

के बाद से काम करता है क्या आप ऑपरेशनबटनक्लिक पर पैरामीटर पास कर सकते हैं? –