I am using maui CommunityToolkit
and I have good working (as I expect) button with CanExecute
:
<code>xaml:
<ImageButton Source="add.png" Command="{Binding LiftLevelUpCommand}" />
</code>
<code>xaml:
<ImageButton Source="add.png" Command="{Binding LiftLevelUpCommand}" />
</code>
xaml:
<ImageButton Source="add.png" Command="{Binding LiftLevelUpCommand}" />
<code>viewmodel:
[RelayCommand(CanExecute = nameof(LiftLevelUpCanExecute))]
private async Task LiftLevelUp()
{
var newValue = LiftValue + sliderIncrement;
if (newValue <= LIFT_MAX_VALUE)
{
LiftValue = newValue;
}
}
private bool LiftLevelUpCanExecute()
{
return LiftValue < LIFT_MAX_VALUE;
}
</code>
<code>viewmodel:
[RelayCommand(CanExecute = nameof(LiftLevelUpCanExecute))]
private async Task LiftLevelUp()
{
var newValue = LiftValue + sliderIncrement;
if (newValue <= LIFT_MAX_VALUE)
{
LiftValue = newValue;
}
}
private bool LiftLevelUpCanExecute()
{
return LiftValue < LIFT_MAX_VALUE;
}
</code>
viewmodel:
[RelayCommand(CanExecute = nameof(LiftLevelUpCanExecute))]
private async Task LiftLevelUp()
{
var newValue = LiftValue + sliderIncrement;
if (newValue <= LIFT_MAX_VALUE)
{
LiftValue = newValue;
}
}
private bool LiftLevelUpCanExecute()
{
return LiftValue < LIFT_MAX_VALUE;
}
And it works fine, when max LiftValue is reached the add.png
button is disabled.
But I want to change behavior of this button with continusly pressed button like:
<code>xaml:
<ImageButton Source="add.png">
<ImageButton.Behaviors>
<toolkit:EventToCommandBehavior
EventName="Pressed"
Command="{Binding LiftLevelUpCommand}"
/>
<toolkit:EventToCommandBehavior
EventName="Released"
Command="{Binding ButtonReleasedCommand}"
/>
</ImageButton.Behaviors>
</ImageButton>
</code>
<code>xaml:
<ImageButton Source="add.png">
<ImageButton.Behaviors>
<toolkit:EventToCommandBehavior
EventName="Pressed"
Command="{Binding LiftLevelUpCommand}"
/>
<toolkit:EventToCommandBehavior
EventName="Released"
Command="{Binding ButtonReleasedCommand}"
/>
</ImageButton.Behaviors>
</ImageButton>
</code>
xaml:
<ImageButton Source="add.png">
<ImageButton.Behaviors>
<toolkit:EventToCommandBehavior
EventName="Pressed"
Command="{Binding LiftLevelUpCommand}"
/>
<toolkit:EventToCommandBehavior
EventName="Released"
Command="{Binding ButtonReleasedCommand}"
/>
</ImageButton.Behaviors>
</ImageButton>
<code>viewmodel:
[RelayCommand(CanExecute = nameof(LiftLevelUpCanExecute))]
private async Task LiftLevelUp()
{
_isButtonHolding = true;
while (_isButtonHolding)
{
var newValue = LiftValue + sliderIncrement;
if (newValue <= LIFT_MAX_VALUE)
{
LiftValue = newValue;
}
await Task.Delay(TimeSpan.FromMilliseconds(50));
}
}
[RelayCommand]
private void ButtonReleased()
{
_isButtonHolding = false;
}
</code>
<code>viewmodel:
[RelayCommand(CanExecute = nameof(LiftLevelUpCanExecute))]
private async Task LiftLevelUp()
{
_isButtonHolding = true;
while (_isButtonHolding)
{
var newValue = LiftValue + sliderIncrement;
if (newValue <= LIFT_MAX_VALUE)
{
LiftValue = newValue;
}
await Task.Delay(TimeSpan.FromMilliseconds(50));
}
}
[RelayCommand]
private void ButtonReleased()
{
_isButtonHolding = false;
}
</code>
viewmodel:
[RelayCommand(CanExecute = nameof(LiftLevelUpCanExecute))]
private async Task LiftLevelUp()
{
_isButtonHolding = true;
while (_isButtonHolding)
{
var newValue = LiftValue + sliderIncrement;
if (newValue <= LIFT_MAX_VALUE)
{
LiftValue = newValue;
}
await Task.Delay(TimeSpan.FromMilliseconds(50));
}
}
[RelayCommand]
private void ButtonReleased()
{
_isButtonHolding = false;
}
But when max value is reached the CanExecute doesn’t work and button is still enabled. How can I fix it? How to use toolkit:EventToCommandBehavior
with CanExecute
.