Closed Axemasta closed 1 year ago
Attaching source code for the samples using 1, 2, 2 vanilla properties :) RelayCommandCanExecuteSample.zip
What about just doing this:
[ObservableProperty]
[NotifyCanExecuteChangedFor(nameof(DoSomethingCommand))]
private bool valueOne;
[ObservableProperty]
[NotifyCanExecuteChangedFor(nameof(DoSomethingCommand))]
private bool valueTwo;
private bool BothToggled => ValueOne && ValueTwo;
[RelayCommand(CanExecute = nameof(BothToggled))]
private void DoSomething()
{
Debug.WriteLine("Doing something...");
}
The property identified by CanExecute
doesn't need to be decorated with ObserableProperty
or NotifyCanExecuteChangedFor
. It gets reevaluated anytime the CanExecuteChanged
event is raised, which the two dependent values take care of.
Ah this is a simpler solution, I didnt consider this approach. This works in my sample so I'm happy to accept this as a working solution 😄
I have lots of legacy code examples which use the Func<bool>
approach to CanExecute
so I just automatically reached for that as a solution for writing the same behaviour in Maui. Thanks for the suggestion!
Overview
Currently the RelayCommand CanExecute takes a property name to use.
Xamarin forms developers, especially those that have used Prism will be used to this sort of concept with Prism's
DelegateCommand
using a similar design. Something that was really useful was the ability to provide a Can Execute function instead of a property, allowing for cleaner code since less boilerplate needs to be written.Consider the following scenario:
Command CanExecute 1 Property
Given the following viewmodel that represents a screen with toggle to enable / disable a command:
The existing api works perfectly and the toggling works, is simple to understand and alot of boilerplate rubbish we used to deal with is gone!
Command CanExecute 2+ Properties
When we add more conditions to our can execute, the code can get a little boilerplatey...
As far as I could manage, you need an extra property to handle the bool logic, you can't for instance have a property that auto calculates this, it must be an
ObservableProperty
:Working in Maui if we did not use the mvvm toolkit, your viewmodel might look something like:
We don't need our extra property since we can pass our command the CanExecute function and all our properties need to do is raise a can execute changed notification for the command to evaluate
Adding the ability to evaluate CanExecute using a function instead of a bool will be useful to developers since it reduces the amount of code they need to write in order to implement more compelx CanExecute statements.
API breakdown
I have no strong opinions on how this api looks but here is a possible example
An added bonus is that when adding more properties, less code will need to be added to ensure
BothToggled
gets updated correctlyUsage example
See above
Breaking change?
No
Alternatives
Correct me if there is a way to do this, but not using the source generators is a way to achieve this however we want the source gens to be the best they can be!
Additional context
No.. I would be happy to contribute towards this change however I would need some help and guidance for specifics. I have built source generators before but this repo's code is particularly scary, a point in the right direction would be greatly appreciated 😇
Help us help you
Yes, but only if others can assist