Mehmet
Mehmet

Reputation: 121

Xamarin.Forms buttons become disabled after touched

I use Xamarin.Forms and MvvmCross, but I've encountered a problem in applications. Buttons become disabled sometimes after touched and running the commands.

I added IsEnabled="True" to button but nothing changed

<Button 
    WidthRequest="36" 
    HeightRequest="36" 
    CornerRadius="18" 
    BorderWidth="2" 
    FontSize="18" 
    Text="{Binding OptionText}" 
    Command="{Binding OptionSelectedCommand}" 
    CommandParameter="{Binding .}" 
    IsEnabled="True" 
    VerticalOptions="Center" 
    HorizontalOptions="Center"/>

I want this button to be enabled always.

My Command code is:

new MvxAsyncCommand(async () => 
{ 
    if (option.IsSelected) 
    { 
        option.IsSelected = false; 
    } 
    else 
    { 
        option.OptionGroup.Options.ForEach(c => c.IsSelected = false);
        option.IsSelected = true; 
    } 

    return Task.CompletedTask; 
})

Upvotes: 5

Views: 1308

Answers (2)

Jeffington
Jeffington

Reputation: 74

Mehmet is correct that the root of this issue is in MvxAsyncCommand. I found that my MvxAsyncCommand's CanExecute() method always returned false. When CanExecute() returns false the Xamarin Forms button becomes disabled, which is the intended behavior. But why does CanExecute() always return false? I dug into the source code and I found that the MvxAsyncCommand's CanExecute() method will return false if it thinks the task is running. If you set allowConcurrentExecutions to true in the constructor for MvxAsyncCommand, it will bypass that check and the button will become enabled again.

This needs to be fixed in MvxAsyncCommand, but setting allowConcurrentExecution = true is a temporary work-around.

MvxAsyncCommand on Github:

public bool CanExecute(object parameter)
{
    if (!_allowConcurrentExecutions && IsRunning)
        return false;
    else
        return CanExecuteImpl(parameter);
}

Upvotes: 0

Mehmet
Mehmet

Reputation: 121

Finally I found a solution about this problem. Problem is related to MvxAsyncCommand, solved by using Command instead of MvxAsyncCommand.

I think MvvmCross MvxAsyncCommand has a bug about running asynchronous methods

Upvotes: 7

Related Questions