Forum Xamarin.Android
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Disable button after clicked

ChrisDrewChrisDrew GBMember ✭✭

Seems like a really simple thing but I can't seem to get it to work. I have a button on a Window which when clicked I would like to be both disabled and have some visual indication that it has been clicked. My button has been set with a 'Style' which sets among other things the background colour.

I thought it was as simple as simply setting the button to not be enabled and setting the background colour as in

void btnJobAction_Click(object sender, EventArgs e)
{
// Disable the button for now
_btnJobAction.Enabled = false;
_btnJobAction.SetBackgroundColor(Android.Graphics.Color.LightGray);

.. do some work 

 _btnJobAction.Enabled = true; 
_btnJobAction.SetBackgroundColor(Android.Graphics.Color.LightBlue); 

}

...but nothing seems to happen even know I know the processing within the routine will take a couple of seconds. I know the attributes are working because if I take out the reset at the end of the handler the button is disabled and gray once I exit from the handler. So I thought it was an issue with the UI not being updated in the handler so I changed the code to

System.Threading.Tasks.Task.Run(() =>
{
this.RunOnUiThread(delegate { _btnJobAction.Enabled = false; _btnJobAction.SetBackgroundColor(Android.Graphics.Color.LightGray); });
});

thinking that by creating a new thread it would work but still no joy. Ultimately I want to encapsulate this behaviour in a separate control but no point if I can't get this to work.

Answers

  • SergTomcatSergTomcat Member ✭✭
    edited October 22

    I tested your idea in this manner:

    private async void Button_Clicked(object sender, EventArgs e)
    {
        var thisBtn = (sender as Button);
        thisBtn.IsEnabled = false;
        thisBtn.BackgroundColor = Color.Gray;
    
        await LongAsyncJob().ConfigureAwait(false);
    
    
        thisBtn.IsEnabled = true;
        thisBtn.BackgroundColor = Color.White;
    
    }
    
    

    and it works as intended.

    Could it be possibly because you invoke event in synchronous way, so your button never recieve contol back to change color and state? Also asynchronous tasks, called from UI, should has ConfigureAwait(false) so main thread is not blocked while waiting for job to be finished.

  • ChrisDrewChrisDrew GBMember ✭✭

    I think you are right - I haven't used much aync code in Xamarin Android - all the time in forms. Can you give an example of the LongAsyncJob? I assume it would be something like

    private Task DoAction
    {
    ..do something

    return null;
    

    }

    As I don't really need the task (or do I for the ConfigureAwait?)

  • ColeXColeX Member, Xamarin Team Xamurai
    edited October 23

    Could you tell us what is some work ? Is it synchronous or Asynchronous ?

    We often use Asynchronous code when invoking some IO operations(file system read/write, network or web access, database access ... etc.).

    The button would never changes if your just invoke synchronous code .

    And , We can't convert synchronous code to asynchronous simply by wrapping the operation into Task.

Sign In or Register to comment.