我的 WPF 项目中有一个按钮,当我按住按钮时,我希望它一遍又一遍地执行相同的命令。我可以使用 RepeatButton,但我的偏好是命令一旦运行完成(在它自己的任务中)就立即再次执行,而不是依赖于 RepeatButton 控件的延迟和间隔属性。
我不介意创建一个按钮单击方法,但命令操作长时间运行,执行时间将取决于 ExecuteParameter 的值(在这种情况下,代表机器物理位置的双精度元组)。
XAML:
<Button FontFamily="Marlett" FontSize="40" Content="5" HorizontalAlignment="Center" VerticalAlignment="Top" Margin="100" Width="100" Height="50"
Command="{Binding IncrBAnglePos}"
CommandParameter="{Binding ElementName=slider, Path=Value}">
</Button>
Run Code Online (Sandbox Code Playgroud)
C#:
SystemCommands.AddSubSystemCommand(SystemRef, CommandNames.IncrAngle, new RelayCommand(
o =>
{
double AngleIncr = (double)o > 5 ? 5 : (double)o;
double nextX = MotionControl.LiveX;
double nextB = MotionControl.LiveB + AngleIncr;
nextB = nextB >= 45 ? 45 : nextB;
Task.Run(() =>
{
SystemCommands.ExecuteCommand(CommandNames.GotoPosition, new Tuple<double,double>(nextX, nextB));
});
},
_ =>
{
if (MotionControl == null)
return false; …Run Code Online (Sandbox Code Playgroud)