locked
How to call a function in xamarin? RRS feed

  • Question

  • User137001 posted

    Please can someone tell me how to call a function in xamarin c# that calls textview or something? Thanks for helpers!

    Tuesday, June 23, 2015 3:09 AM

All replies

  • User209 posted

    It is no different than "regular" C#.

    A simply example:

    void Derp(TextView durr, string herp)
    {
        durr.Text = herp;
    }
    

    Then you can call that from whereever you want passing in the correct arguments. I.e.:

    Derp(myTextView, "Herp Derp");
    
    Tuesday, June 23, 2015 7:16 AM
  • User137001 posted

    If I got a bigger code and I have in it a lot of arguments how can call the all method? If you want to see my code check out my profile I got another forum question

    Tuesday, June 23, 2015 12:34 PM
  • User209 posted

    I don't understand what the problem is. Post some sample code here.

    Tuesday, June 23, 2015 2:11 PM
  • User137001 posted

    Ok here is my calculator machine code that I built in visual studio and broth it to xamarin

    Mainactivity.cs using System;

    using Android.App; using Android.Content; using Android.Runtime; using Android.Views; using Android.Widget; using Android.OS;

    namespace Firstapp {

    [Activity (Label = "Firstapp", MainLauncher = true, Icon = "@drawable/icon")]

    public class MainActivity : Activity { //int count = 1;

    protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);
    
        // Set our view from the "main" layout resource
        SetContentView (Resource.Layout.Main);
    
        // Get our button from the layout resource,
        // and attach an event to it
        calc();
    }
    private void calc()
    {
    
        Button button0 = FindViewById<Button> (Resource.Id.button0);
        Button button1 = FindViewById<Button> (Resource.Id.button1);
        Button button2 = FindViewById<Button> (Resource.Id.button2);
        Button button3 = FindViewById<Button> (Resource.Id.button3);
        Button button4 = FindViewById<Button> (Resource.Id.button4);
        Button button5 = FindViewById<Button> (Resource.Id.button5);
        Button button6 = FindViewById<Button> (Resource.Id.button6);
        Button button7 = FindViewById<Button> (Resource.Id.button7);
        Button button8 = FindViewById<Button> (Resource.Id.button8);
        Button button9 = FindViewById<Button> (Resource.Id.button9);
        Button buttonmultiplay = FindViewById<Button> (Resource.Id.buttonMultiply);
        Button buttondevide = FindViewById<Button> (Resource.Id.buttondivide);
        Button buttonplus = FindViewById<Button> (Resource.Id.buttonplus);
        Button buttonminus = FindViewById<Button> (Resource.Id.buttonminus);
        Button buttonpoint = FindViewById<Button> (Resource.Id.buttonpoint);
        Button buttonequals = FindViewById<Button> (Resource.Id.buttonequals);
        TextView Maintext = FindViewById<TextView> (Resource.Id.Mytextview);
    
    
        button0.Click += delegate {
            Maintext.Text = Maintext.Text + "0";
            //button.Text = string.Format ("{0} clicks!", count++);
        };
        button1.Click += delegate {
            Maintext.Text = Maintext.Text + "1";
            ;
    
        };
        button2.Click += delegate {
            Maintext.Text = Maintext.Text + "2";
            ;
    
        };
        button3.Click += delegate {
            Maintext.Text = Maintext.Text + "3";
            ;
    
        };
        button4.Click += delegate {
            Maintext.Text = Maintext.Text + "4";
            ;
    
        };
        button5.Click += delegate {
            Maintext.Text = Maintext.Text + "5";
            ;
    
        };
        button6.Click += delegate {
            Maintext.Text = Maintext.Text + "6";
            ;
    
        };
        button7.Click += delegate {
            Maintext.Text = Maintext.Text + "7";
            ;
    
        };
        button8.Click += delegate {
            Maintext.Text = Maintext.Text + "8";
    
        };
        button9.Click += delegate {
            Maintext.Text = Maintext.Text + "9";
    
        };
    
        buttonmultiplay.Click += delegate {
            Maintext.Text = Maintext.Text + "*";
    
        };
        buttondevide.Click += delegate {
            Maintext.Text = Maintext.Text + "/";
    
        };
        buttonplus.Click += delegate {
            Maintext.Text = Maintext.Text + "+";
    
        };
        buttonminus.Click += delegate {
            Maintext.Text = Maintext.Text + "-";
    
        };
        buttonpoint.Click += delegate {
            Maintext.Text = Maintext.Text + ".";
            string ezer = Maintext.Text;
            int len = ezer.Length;
            if (ezer [len - 1] != '.') {
                Maintext.Text = Maintext.Text + ".";
            }
        };
    
        //----------------------------------------------------//
        //----------------------Equals------------------------//
    
        buttonequals.Click += delegate {
            Maintext.Text = Maintext.Text + "=";
            string check = "=";
    
            if (Maintext.Text.IndexOf (check) == -1) {
                Maintext.Text = Maintext.Text + "=";
    
                double result = 0;
                string value = "";
                char sign = '+';
                double acc_val = 0;
    
                Pointer p = new Pointer ();
                p.targil = Maintext.Text;
                p.i = 0;
                if (p.targil [0] == '-') {
                    value = "-";
                    p.i = 1;
                }
                for (; p.i < p.targil.Length && sign != '='; p.inc ()) {
                    if (p.targil [p.i] != '+' && p.targil [p.i] != '-' && p.targil [p.i] != '=' && p.targil [p.i] != '*' && p.targil [p.i] != '/') { //this is a number
                        value = value + p.targil [p.i];
                    } else {  //this is a sign
    
                        result = Convert.ToDouble (value);
                        value = "";
    
                        if (p.targil [p.i] == '*' || p.targil [p.i] == '/') {//we are doing * or /
    
                            switch (sign) {
                            case '+':
                                acc_val = acc_val + md_ops (result, p.targil [p.i], ref p);
                                break;
                            case '-':
                                acc_val = acc_val - md_ops (result, p.targil [p.i], ref p);
                                break;
    
                            }
    
                            // sign = ' ';
                        } //end of else if for * or /
                        else { // sign is + or -
                            switch (sign) {
                            case '+':
                                acc_val = acc_val + result;
                                break;
                            case '-':
                                acc_val = acc_val - result;
                                break;
    
                            } //end switch
    
                        } //end else sign is + or -
                        sign = p.targil [p.i];
                        //Console.WriteLine("Result " + acc_val);
                    } //end else this is a sign +/-
    
                }//end of loop
    
                Maintext.Text = p.targil + acc_val;
    
                //Maintext.Text = "" + acc_val;
    
            }
        };
    }
    
    
    
    
    static double md_ops(double res, char sn, ref Pointer p)
    {
        double toz = 0;
        char sign = ' ';
        string value = "";
        do
        {
            p.i++;
    
            if (p.targil[p.i] != '+' && p.targil[p.i] != '-' && p.targil[p.i] != '=' && p.targil[p.i] != '*' && p.targil[p.i] != '/') //this is a number
            {
                value = value + p.targil[p.i];
            }
            else  //this is a sign +/-
            {
    
                double result = Convert.ToDouble(value);
                value = "";
                //   sign = p.targil[p.i];   - *** it was there before
                switch (sn)
                {
                case '*':
                    toz = res * result;
                    break;
                case '/':
                    toz = res / result;
                    break;
    
                }
    
                if (p.targil[p.i] == '*' || p.targil[p.i] == '/')//we are doing * or /
                {
                    toz = md_ops(toz, p.targil[p.i], ref p);
                } //end of else if for 
    
                sign = p.targil[p.i];  // - *** it is now here
            } //end of else this is a sign +/-
    
        } while (sign != '+' && sign != '-' && sign != '=');
        return (toz);
    
    }
    
    
    }
    

    } Pointer.cs (class) using System;

    namespace Firstapp { public class Pointer { public int i; public string targil; public int count = 0;

    public void inc()
    {
        if (targil[i] != '=')
        {
            i++;
    
        }
    
    }
    

    } }

    So I'm calling the function calc() and when I debug the all thing it runs the app but when I press = it doesn't do eny thing I need help please man

    Tuesday, June 23, 2015 7:06 PM