Invoking a method from another method

There are two button methods as below:

private void Start_Clicked(object sender, EventArgs e)
{
try
{
_speechRecongnitionInstance.StartSpeechToText();
}
catch (Exception ex)
{
input.Text = ex.Message;
}
if (Device.RuntimePlatform == Device.iOS)
{
start.IsEnabled = false;
}
}

private void Submit_Clicked(object Sender, EventArgs args)
{
var input_text = input.Text;
webapicall(input_text).Wait();
output.Text = output_text;
CrossTextToSpeech.Current.Speak(output_text);
LogTable ld = new LogTable
{
Username = username,
Question = input_text,
Answer = output_text,
Timestamp = DateTime.Now.ToString()
};
int i = App.Database.SaveItem(ld);
}

How to I call second method from first method?
I tried but I am facing problem, without executing first method, second method is invoked.

Answers

  • kentuckerkentucker USMember ✭✭✭✭✭

    The button click events are just functions. You can just call them like another method

    private void Submit_Clicked(object Sender, EventArgs args)
    {
            Start_Clicked(sender, args);
    
  • yelinzhyelinzh Member, Xamarin Team Xamurai
    edited April 26

    Try the following code.

    public partial class Page1 : ContentPage
    {
        public Page1()
        {
            InitializeComponent();
        }
    
        private async void Btn1_Clicked(object sender, EventArgs e)
        {
            method1();
    
            method2();
        }
        private void Btn2_Clicked(object sender, EventArgs e)
        {
            method2();
        }
        void method1()
        {
            //
        }
        void method2()
        {
            //
        }
    }
    
  • igorkr_10igorkr_10 Member ✭✭✭
    edited April 26

    You need to create another method which will be called from your event callback (check post below). And you need to learn C# fundamentals (this or this or another sources).

  • RakeshDasapathriRakeshDasapathri Member ✭✭

    @igorkr_10 said:
    You need to create another method which will be called from your event callback (check post below). And you need to learn C# fundamentals (this or this or another sources).

    Can you please elaborate the procedure running two methods.

  • igorkr_10igorkr_10 Member ✭✭✭

    @RakeshDasapathri said:

    @igorkr_10 said:
    You need to create another method which will be called from your event callback (check post below). And you need to learn C# fundamentals (this or this or another sources).

    Can you please elaborate the procedure running two methods.

    Do you want run two methods asynchronously or one after other?

  • RakeshDasapathriRakeshDasapathri Member ✭✭

    @yelinzh said:
    Try the following code.

    public partial class Page1 : ContentPage
    {
        public Page1()
        {
            InitializeComponent();
        }
    
        private async void Btn1_Clicked(object sender, EventArgs e)
        {
            method1();
    
            method2();
        }
        private void Btn2_Clicked(object sender, EventArgs e)
        {
            method2();
        }
        void method1()
        {
            //
        }
        void method2()
        {
            //
        }
    }
    

    Its not working as my first method has multiple tasks of taking speech and converting it to text.
    The second method should be called after all the work(speech input) done by the first method.

  • RakeshDasapathriRakeshDasapathri Member ✭✭

    @kentucker said:
    The button click events are just functions. You can just call them like another method

    private void Submit_Clicked(object Sender, EventArgs args)
    {
            Start_Clicked(sender, args);
    

    Its not working as my first method has multiple tasks of taking speech and converting it to text.
    The second method should be called after all the work(speech input) done by the first method.

  • RakeshDasapathriRakeshDasapathri Member ✭✭

    @igorkr_10 said:

    @RakeshDasapathri said:

    @igorkr_10 said:
    You need to create another method which will be called from your event callback (check post below). And you need to learn C# fundamentals (this or this or another sources).

    Can you please elaborate the procedure running two methods.

    Do you want run two methods asynchronously or one after other?

    I want to run one after the other.

    After the first method(speech input), I want second method to be invoked.

Sign In or Register to comment.