maui icon indicating copy to clipboard operation
maui copied to clipboard

Setting 'CanExecute' doesn't change the status of Button controls when they are bound to MVVM Commands

Open DRAirey1 opened this issue 1 year ago • 44 comments

Description

I have commands that are wired up to buttons through a view model. When the user hits "Buy AAPL", an order is generated for Apple, and the order is sent to the server. While the order is executing, I want this button disabled so they don't attempt to buy Apple stock again. To do this, I clear the 'CanExecute' flag on the command that's bound to the button. This part appears to work properly as the button is greyed out and clicking on it does nothing.

However, when the command is finished and I set the 'CanExecute' flag and call the 'CanExecuteChanged' event handler again, the status of the UI control is not updated, although the button will respond to click events again.

Oddly, the other buttons that I disabled and re-enabled during this operation are updated correctly. It's only the button bound to the command that is not updated.

Steps to Reproduce

  1. Build the attached solution.
  2. Run it.
  3. Hit the button.

Version with bug

6.0.400

Last version that worked well

Unknown/Other

Affected platforms

iOS, Windows

Affected platform versions

iOS15, Windows SDK 10

Did you find any workaround?

Yes, if you schedule a task to run on the main windows thread to handle the command, it appears to work.

Relevant log output

No response

DRAirey1 avatar Aug 29 '22 17:08 DRAirey1

ButtonBug.zip

DRAirey1 avatar Aug 29 '22 17:08 DRAirey1

From a quick debug, the button is enabled and disabled correctly, but background color changes are not notified to the handler from the VisualState.

jsuarezruiz avatar Aug 30 '22 10:08 jsuarezruiz

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

msftbot[bot] avatar Aug 30 '22 18:08 msftbot[bot]

Thanks for the report @DRAirey1! I have two questions:

  1. You report the version number you're using as 6.0.400, are you on the latest Visual Studio? What is the Visual Studio version you have and what does dotnet --version tell you?
  2. Could I maybe ask you to add the repro as a GitHub repo? We're unfortunately not allowed to accept zip reproductions.

jfversluis avatar Aug 30 '22 18:08 jfversluis

Hi @DRAirey1. 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.

msftbot[bot] avatar Aug 30 '22 18:08 msftbot[bot]

@jfversluis

Created https://github.com/Delphin-Technology/maui_9753_repo as a reproduction repository.

We are able to reproduce the issue by using CommunityToolkit.Mvvm [RelayCommand] on a async Task method.

Visual Studio 2022 Version is 17.3.3 dotnet --version tells 6.0.400

DelphinRP avatar Sep 02 '22 07:09 DelphinRP

I have exactly the same issue using CommunityToolkit.Mvvm

achilleaskar avatar Sep 06 '22 17:09 achilleaskar

Also ran into the same issue, was very confused because at first I thought my code was wrong... Maybe it helps to let you know that, this kind of button will still work properly in an android app...

PLEXATIC avatar Sep 14 '22 20:09 PLEXATIC

I commented my custom style and it works now....

achilleaskar avatar Sep 14 '22 21:09 achilleaskar

@achilleaskar Can you explain more in detail how you solved it, ive tried all kinds of styles and methods, and i cannot get the background to update the color.

RonnyRos avatar Sep 27 '22 08:09 RonnyRos

@achilleaskar Can you explain more in detail how you solved it, ive tried all kinds of styles and methods, and i cannot get the background to update the color.

I just didn't use the custom style... Left the default. I spent many hours trying to fix this and it didn't worth it. I haven't tried if it works in the .Net 7 version tho, maybe I will try it today and feedback here

achilleaskar avatar Sep 27 '22 08:09 achilleaskar

I have a similar issue with my buttons bound to commands with CanExecute functionality. My buttons have various stylings for background colors. When compiled for Debug, everything works as expected. When compiled for Release, the buttons' BackgroundColor do not update. However, the buttons' IsEnabled states update.

So, things technically work but the users are going to be really confused if I release the app like this. I'd rather not have to go through the application and overlay a second disabled button on top of all my existing buttons but I might have to.

adamjhilton avatar Oct 18 '22 13:10 adamjhilton

Quick update... For sh*ts and giggles, I ran the Release build in a "Windows Machine" and the buttons behaved normally. So, this is just an issue with Android. (I have no idea how iOS behaves.) Also, this issue persists in both my VS emulator and on a physical device where the .APK has been sideloaded.

adamjhilton avatar Oct 18 '22 13:10 adamjhilton

I do use the same RelayCommand with CanExecute method and C# Markup instead of xaml.

Button is clickable as expected but colors does not change.

It does not work on both debug and release builds on Windows Machine.

Mzazvor avatar Oct 26 '22 10:10 Mzazvor

Also confirmed same behavior. On a windows machine the button works, but shows disabled. If you right click on the button, the button will appear enabled.

Visual Studio 17.3.5 dotnet --version 6.0.401

nmushovic avatar Nov 03 '22 16:11 nmushovic

So looking into this it seems that this indeed is a visual thing. The button is in fact enabled, but it doesn't show visually.

However, on the latest version of .NET 6 and now on .NET 7, it seems to work (with the reproductions here) on iOS/macOS and Android, but not yet on Windows.

Does that seem correct?

jfversluis avatar Nov 11 '22 13:11 jfversluis

However, on the latest version of .NET 6 and now on .NET 7, it seems to work (with the reproductions here) on iOS/macOS and Android, but not yet on Windows. Does that seem correct?

No. Just the opposite. It works just fine in Windows. It even seems to work on the iOS simulators, but it fails when you put the actual code on the device (either through the App Store or Ad Hoc loading).

DRAirey1 avatar Nov 11 '22 13:11 DRAirey1

That's very interesting, because your example is only not working on Windows for me 😄 but the physical devices angle is interesting to. Need to do some testing then.

But just to clarify, you did upgrade to newer versions and you still see this?

jfversluis avatar Nov 11 '22 14:11 jfversluis

Upgrade to .net 7 did not solve it for me.

Mzazvor avatar Nov 12 '22 08:11 Mzazvor