Xamarin.Forms buttons become disabled after touched
Asked Answered
H

2

5

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; 
})
Heraldry answered 3/1, 2019 at 7:47 Comment(4)
What does the code for your Command look like?Confabulation
Just to expand on Gerald's comment, what is the code you have in OptionSelectedCommand?Undergarment
this is my OptionSelectedCommand new MvxAsyncCommand(async () => { if (option.IsSelected) { option.IsSelected = false; } else { option.OptionGroup.Options.ForEach(c=>c.IsSelected=false); option.IsSelected = true; } return Task.CompletedTask; }); @GeraldVersluisHeraldry
This may be related: github.com/MvvmCross/MvvmCross/issues/1589.Undergarment
H
7

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

Heraldry answered 4/1, 2019 at 9:15 Comment(4)
Not sure why your answer was downvoted. I had the same issues. The button becomes disabled, not always, but most of the time after executing. Thanks for the tip, changing from MvxAsyncCommand to MvxCommand fixed it for me too.Madura
Unfortunately, the issue is still actual even as of MvvmCross 6.4.0, which is quite embarrassing.Nave
And workaround just eliminates the issue, however, the command becomes un-async then, and it doesn't lock the button which can be pressed again.Nave
Still an issue with 6.4.1.Schema
O
0

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);
}
Orsola answered 5/3, 2020 at 17:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.