dotnet icon indicating copy to clipboard operation
dotnet copied to clipboard

Command does not exist in the current context WPF

Open DirkKramer opened this issue 3 years ago • 3 comments

Describe the bug

Cant find Command in the current context. In my specific example included in the 'Steps to reproduce' the compiler cant find CloseProjectCommand.

Regression

No response

Steps to reproduce

'''
     public bool HasProject => activeProject != null;


        [ObservableProperty]
        [AlsoNotifyCanExecuteFor(nameof(CloseProjectCommand))]
        Project activeProject;


        [ICommand(CanExecute = nameof(HasProject))]
        void CloseProject()
        {
            //Close active project
        }
'''

Expected behavior

Command should be able to be found by the compiler.

Screenshots

No response

IDE and version

VS 2022

IDE version

Version 17.1.3

Nuget packages

  • [ ] CommunityToolkit.Common
  • [ ] CommunityToolkit.Diagnostics
  • [ ] CommunityToolkit.HighPerformance
  • [X] CommunityToolkit.Mvvm (aka MVVM Toolkit)

Nuget package version(s)

8.0.0-preview3

Additional context

No response

Help us help you

No, just wanted to report this

DirkKramer avatar Apr 21 '22 09:04 DirkKramer

I'm experiencing a similar problem, I guess...: given this Delete method

[ICommand(CanExecute = nameof(CanDelete))]
private async Task Delete()
{
    var canBeDeleted = await CanDelete();
    if (canBeDeleted && SelectedOperator is not null)
    {
        using var channel = new ClientChannel();
        var request = new DeleteOperatorRequest() { OperatorId = SelectedOperator.OperatorId };
        bool deleted = await channel.DeleteOperator(request).ConfigureAwait(true);
        if (deleted)
        {
            await LoadOperatorsList();
        }
    }
}

and this CanDelete method:

private async Task<bool> CanDelete()
{
  if (SelectedOperator is not null)
  {
      using var client = new ClientChannel();
      var response = await client.OperatorHasAppointments(new HasAppointmentsRequest
      { OperatorId = SelectedOperator.OperatorId }).ConfigureAwait(true);
      return !response.HasAppointments;
  }

  return false;
}

I get the following compilation error: The CanExecute name must refer to a compatible member, but no valid members were found for CanDelete I don't know If I messed up somewhere or it is the same bug

robertodalmonte avatar Jun 04 '22 15:06 robertodalmonte

@robertodalmonte That's by design, and the error message is telling you what the issue is. Your CanExecute member is a method returning Task<bool>, which isn't valid. It needs to be either a property or a synchronous method (returning bool).

Sergio0694 avatar Jun 04 '22 15:06 Sergio0694

Thank you, Sergio. Thanks to your explanation I was now able to find a viable solution.

robertodalmonte avatar Jun 04 '22 16:06 robertodalmonte

Closing this, the minimal repro provided by OP doesn't actually reproduce the issue. I'd recommend making sure to use the latest stable release of the MVVM Toolkit and also checking that the containing type of those methods and properties is marked as partial.

Sergio0694 avatar Dec 18 '22 02:12 Sergio0694