How do i call async method from a button

2019-07-04 01:42发布

问题:

I am trying to call a aysnc method using latest xamrian in alpha using story board designer but i cant seem to call the method from a button click that has been declared by the desginer. CS

async public  void Createuser ()
{        
    var user = new ParseUser ()
    {
        Username = txtUserName.Text,
        Password = txtPassword.Text,
        Email = txtEmail.Text
    };

    await user.SignUpAsync ();
}

the button which i wish to call the method from is this

partial void btnSave_TouchUpInside (UIButton sender)
{
    Createuser();        
}

But if we look at the following i the designer.cs file

[Action ("btnSave_TouchUpInside:")]
[GeneratedCode ("iOS Designer", "1.0")]
partial void btnSave_TouchUpInside (UIButton sender);

回答1:

Based on your question, I assume that you can't just add the async modifier to the event handler as such?

async partial void btnSave_TouchUpInside (UIButton sender)
{
    await Createuser();        
}

I generally do my clicks inline using a lambda:

btnSave.TouchUpInside += async (sender, e) => 
{
    await Createuser(); 
};