maui icon indicating copy to clipboard operation
maui copied to clipboard

The command interface for buttons - Command.ChangeCanExecute()

Open akhegr opened this issue 2 years ago • 5 comments

The method call Command.ChangeCanExecute() is explained in the introduced in the guide for buttons in MAUI - link is shown below. https://docs.microsoft.com/en-us/dotnet/maui/user-interface/controls/button#use-the-command-interface

When you use the code right above the title "Press and release the button".

MultiplyBy2Command = new Command(
                execute: () =>
                {
                    Number *= 2;
                    ((Command)MultiplyBy2Command).ChangeCanExecute();
                    ((Command)DivideBy2Command).ChangeCanExecute();
                },
                canExecute: () => Number < Math.Pow(2, 10));

            DivideBy2Command = new Command(
                execute: () =>
                {
                    Number /= 2;
                    ((Command)MultiplyBy2Command).ChangeCanExecute();
                    ((Command)DivideBy2Command).ChangeCanExecute();
                },
                canExecute: () => Number > Math.Pow(2, -10));

The app is in Windows showing a fine blue button, that changes color from blue to gray, when it reaches the limit. When you leave the limit again, the button do not change the color, but it is again clickable.

If you reach, the upper limit, and afterwards the lower limit, both buttons are gray, as shown on the picture below. image

akhegr avatar Aug 14 '22 19:08 akhegr

Thanks for the report @akhegr I am not really clear though on what the issue here. Could you maybe elaborate a bit more about what you expect to see and what you're actually seeing?

Do I understand correctly that the button shows as disabled but it actually isn't disabled? Can you reproduce this outside of this sample code in the docs as well? On which platforms do you see this?

jfversluis avatar Aug 17 '22 08:08 jfversluis

Hi @akhegr. We have added the "s/needs-info" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

ghost avatar Aug 17 '22 08:08 ghost

The project is based on the section "Use the command interface" on the earlier referred page about Buttons in MAUI.

From start, both of the buttons are blue and clickable. When I continuously click on the "Multiply by 2" button, until I reach the limit (1024), the button change color to gray as expected, and is deactivated. Afterwards, I clicked on the "Divide by 2" button, and the Multiply button is now clickable, but the color do not change.

When you continuously click on the "Divide by 2" button, until it reach its limit (0,0009765625), it changes color to grey too, but it neither change back to original color, when it is clickable again.

Now sometimes, when I click a little randomly forward and backward, the button changes color to blue again.

Furthermore, today, I have updated to the latest released version of Visual Studio, and Visual Studio is now using much CPU power from start to execute or build the project. The Build and Run button are also enabled, before the previous told task is done, and when you click a second time before the execution is done, it return a critical error. After a long while, running the project, it is opened now, and I have recorded a video of the MAUI bug with Buttons

https://user-images.githubusercontent.com/10374847/185479561-89386234-7c81-408d-ac14-f05a36b93ff0.mp4

akhegr avatar Aug 18 '22 18:08 akhegr

Also observing this same issue with near identical, just simpler with a toggle. Is there an expected timeline for this fix?

Toggle

Spilchuk avatar Mar 09 '23 03:03 Spilchuk

Because of this, I kind of gave up on having 2 buttons enable and disable each other. Instead, I combined them but now while in one of those states, I just strictly stick with enabling and disabling one, not both.

Retry1

Here's a snippet of (effectively) the code here:

image

So happy this condition works instead! Hope this helps troubleshooting.

Spilchuk avatar Mar 11 '23 02:03 Spilchuk

Also observing this same issue with near identical, just simpler with a toggle. Is there an expected timeline for this fix?

Toggle Toggle

Can you try to reproduce this issue with the latest version of .NET 8?

japarson avatar Aug 31 '23 20:08 japarson

This is now fixed in .NET 8 and probably was fixed in https://github.com/dotnet/maui/pull/12488

mattleibow avatar Nov 01 '23 18:11 mattleibow