Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using Command CanExecute #84

Open
pedroivobraga opened this issue Jan 21, 2021 · 2 comments
Open

Using Command CanExecute #84

pedroivobraga opened this issue Jan 21, 2021 · 2 comments

Comments

@pedroivobraga
Copy link

pedroivobraga commented Jan 21, 2021

Hi,
I'm trying to use the AsyncCommand with CanExecute, but I'm not sure how to revalidate the CanExecute.
I'm calling RaiseCanExecuteChanged, but it is not working.

Anyone could help me?

I'm using Xamarin.Forms v5.0

@robertodalmonte
Copy link

Hi Pedro,
I run into the same issue myself...I guess the problem is that the IAsyncCommand interface (in the NuGet package) is missing the RaiseCanExecuteChanged() method, while the mvvm-helpers repository has it.
So in order to update the CanExecute method every time a property changes, you have to declare the command using the class, not the interface. The class has the method, the interface does not.

internal class CheckViewModel : ViewModelBase
{
private AsyncCommand CheckCommand { get; }
//private IAsyncCommand CheckCommand {get;}//doesn't work
public CheckViewModel()
{
CheckCommand = new AsyncCommand(CheckOfficeExists, CanCheckOfficeExists);
this.PropertyChanged += (_, _) => CheckCommand.RaiseCanExecuteChanged();
}

    private bool CanCheckOfficeExists(object args)
    {
        return IsValid;
    }

    private async Task CheckOfficeExists()
    {
          DoSomething();
    }

@robertodalmonte
Copy link

oops...the command must be public
public AsyncCommand CheckCommand { get; }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants