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

CanExecute can not change Button clickability #22652

Closed
CodingOctocat opened this issue May 26, 2024 · 9 comments
Closed

CanExecute can not change Button clickability #22652

CodingOctocat opened this issue May 26, 2024 · 9 comments
Assignees
Labels
area-controls-button Button, ImageButton platform/android 🤖 s/triaged Issue has been reviewed s/verified Verified / Reproducible Issue ready for Engineering Triage t/bug Something isn't working
Milestone

Comments

@CodingOctocat
Copy link

CodingOctocat commented May 26, 2024

Description

This issue has been mentioned but not resolved.
Similar issues, See:

  1. Setting 'CanExecute' doesn't change the status of Button controls when they are bound to MVVM Commands #9753
  2. Command.CanExecute does not disable Button #5364
  3. Button Command CanExecute does not update the button visual on Windows #6297

In my case I use Android/MvvmToolkit, [RelayCommand(CanExecute = nameof(IsValidForm))], if IsValidForm(ObservableProperty) is initially true, the button is clickable, if false, the button is not clickable, and changes to the value of IsValidForm don't change the state of the button (not the visual style, but the clickability).

Steps to Reproduce

No response

Link to public reproduction project repository

No response

Version with bug

8.0.40 SR5

Is this a regression from previous behavior?

Yes, this used to work in .NET MAUI

Last version that worked well

Unknown/Other

Affected platforms

Android, I was not able test on other platforms

Affected platform versions

No response

Did you find any workaround?

No response

Relevant log output

No response

@CodingOctocat CodingOctocat added the t/bug Something isn't working label May 26, 2024
Copy link
Contributor

Hi I'm an AI powered bot that finds similar issues based off the issue title.

Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one and thumbs upping the other issue to help us prioritize it. Thank you!

Closed similar issues:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.

@CodingOctocat CodingOctocat changed the title CanExecute can not change Button availability CanExecute can not change Button clickability May 26, 2024
@RoiChen001 RoiChen001 added s/verified Verified / Reproducible Issue ready for Engineering Triage s/triaged Issue has been reviewed labels May 27, 2024
@RoiChen001
Copy link
Collaborator

Can repro this issue at Android platform on the latest 17.11.0 Preview 1.0(8.0.7/8.0.10/8.0.21).
BindingBreaksStyle.zip

@mattleibow mattleibow added s/needs-info Issue needs more info from the author s/needs-repro Attach a solution or code which reproduces the issue labels May 27, 2024
@mattleibow
Copy link
Member

This looks related: #9753

@dotnet dotnet deleted a comment from dotnet-policy-service bot May 27, 2024
@dotnet dotnet deleted a comment from dotnet-policy-service bot May 27, 2024
@mattleibow mattleibow added area-controls-button Button, ImageButton and removed s/needs-info Issue needs more info from the author s/needs-repro Attach a solution or code which reproduces the issue labels May 27, 2024
@mattleibow mattleibow added this to the Backlog milestone May 27, 2024
@mattleibow mattleibow self-assigned this May 27, 2024
@CodingOctocat
Copy link
Author

This looks related: #9753

Not quite the same, #9753 case is that the visual of the button has not changed, but in my case the button cannot be enabled/disabled based on the CanExecute change.

@drasticactions
Copy link
Contributor

FWIW I was able to get it working just fine

https://github.com/drasticactions/MauiRepros/tree/main/MvvmTestCanExecute

スクリーンショット 2024-05-28 16 08 48 スクリーンショット 2024-05-28 16 08 59

It may be that you didn't set up your model correctly? Maybe the library you are using is at fault? If you make a sample showing what you're doing that could help, or maybe run what I wrote above and see if it doesn't work for you?

@CodingOctocat
Copy link
Author

CodingOctocat commented May 28, 2024

@drasticactions Hi, I made a sample, I use Mvvm Toolkit, and the [RelayCommand] feature.

public partial class MainViewModel : ObservableObject
{
    [ObservableProperty]
    private bool _canDoWork;  // binding to Switch button

    [RelayCommand(CanExecute = nameof(CanDoWork))]
    private static void DoWork()  // binding to 'Click me' button
    {
        Debug.WriteLine("DoWork");
    }
}

动画

MauiIssue22652.zip

@drasticactions
Copy link
Contributor

drasticactions commented May 28, 2024

Thank you for the sample! I believe you set up your view model incorrectly.

First, in your app page, your View Model isn't set as the BindingContext, it's null, so it will never work.

Then, in your ViewModel,

public partial class MainViewModel : ObservableObject
{
    [ObservableProperty]
    private bool _canDoWork;

    [RelayCommand(CanExecute = nameof(CanDoWork))]
    private static void DoWork()
    {
        Debug.WriteLine("DoWork");
    }
}

I am not well versed in the CommunityToolkit MVVM, but I believe you didn't set it up correctly. CanExecute gets called whenever someone calls for it to be raised, it's not done automatically. So when CanDoWork gets updated, you then need to raise the event on DoWork to be able to call CanExecute.

Changing it to this:

public partial class MainViewModel : ObservableObject
{
    [ObservableProperty]
    private bool _canDoWork;
    
    partial void OnCanDoWorkChanged(bool value)
    {
       this.DoWorkCommand.NotifyCanExecuteChanged();
    }

    [RelayCommand(CanExecute = nameof(CanDoWork))]
    private async void DoWork()
    {
        Debug.WriteLine("DoWork");
    }
}

Allows it to work. Likewise, DoWork can't be static, or else you can't call on the command for NotifyCanExecuteChanged. I would encourage you to read their documentation for more about it, but I do not believe this is a MAUI bug.

スクリーンショット 2024-05-28 17 23 59 スクリーンショット 2024-05-28 17 23 53

@CodingOctocat
Copy link
Author

@drasticactions Thanks! In my sample I did forget to set BindingContext, I didn't actually forget this in my actual project, the main problem was that I forgot to add [NotifyCanExecuteChangedFor(nameof(DoWorkCommand))].

@drasticactions
Copy link
Contributor

I'm glad you solved it! I'm also pleased that NotifyCanExecuteChangedFor exists; since my solution was kind of janky, that one makes more sense.

@github-actions github-actions bot locked and limited conversation to collaborators Jun 27, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-controls-button Button, ImageButton platform/android 🤖 s/triaged Issue has been reviewed s/verified Verified / Reproducible Issue ready for Engineering Triage t/bug Something isn't working
Projects
Status: Done
Development

No branches or pull requests

4 participants