ACl*_*ark 5 c# wpf relaycommand
我的 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;
return !MotionControl.InMotionCheckStatus;
}));
if (MotionControl != null)
{
MotionControl.MotionChanged += SystemCommands.GetRelayCommand(CommandNames.IncrAngle).CanExecutePropertyChangedNotification;
}
Run Code Online (Sandbox Code Playgroud)
更新: 我可以看到有几个人在看一眼。如果有人对我如何改进问题本身有建议,我会欢迎反馈。由于我缺乏声誉,您可能会猜测我是这方面的新手。
重复按钮应该可以满足您的要求
间隔-> 重复开始后重复之间的时间量(以毫秒为单位)。该值必须是非负数。
延迟-> 重复按钮在按下后开始重复之前等待的时间(以毫秒为单位)。
<RepeatButton FontFamily="Marlett"
Delay="500"
Interval="100"
FontSize="40"
Content="5"
HorizontalAlignment="Center"
VerticalAlignment="Top"
Margin="100"
Width="100"
Height="50"
Command="{Binding IncrBAnglePos}"
CommandParameter="{Binding ElementName=slider, Path=Value}">
</RepeatButton>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1042 次 |
| 最近记录: |