简体   繁体   中英

Raise control event programmatically in Xamarin.Forms

I have a button defined in XAML:

<Button x:Name="loginButton" Text="Login" Clicked="OnLoginButtonClicked" />

Is there a possibility to raise the Clicked event programmatically? Of course I could call OnLoginButtonClicked , but I'm interested how the Clicked event itself can be raised.

If you just want to call the Clicked action, you can do this trick:

    var b = new Button();

    b.Clicked += (x, y) =>
    {
        //Your method here
    };

    var t = b as IButtonController;

    t.SendClicked(); //This will call the action

It is important to note this is not the right one. As it was mentioned before, calling the actual method is preferred.

You can call DoSomething by event handler or any other place in your code

void OnLoginButtonClicked(object sender, EventArgs e)
{
    DoSomething ();
}

private void DoSomething()
{
    //Click code here.
}
  1. Assing a delegate method

     testButton3.TouchUpInside += HandleTouchUpInside; 
  2. Add the method

     void HandleTouchUpInside (object sender, EventArgs ea) { new UIAlertView("Touch3", "TouchUpInside handled", null, "OK", null).Show(); } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM