The Enable button: enable an axis
This button functions as an Enable or Disable button, depending on the axis’ enabled or disabled status. Click Enable to enable an axis that is not already enabled. Click Disable to disable an axis that is already enabled.
The method of this button is BtnEnable_Click (MainWindow.xaml.cs). When Enable or Disable is clicked, we call the EnableAxis method (MainWindowViewModel.cs), in which we check whether the selected axis has been enabled. If it has, this button functions as Disable, otherwise it functions as Enable.
private async void BtnEnable_Click(object sender, RoutedEventArgs e)
{
try
{
await ViewModel.EnableAxis();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
The Enable button is available only when an axis is selected. We bind the IsEnabled attribute with the variable IsAxisSelected, which checks whether an axis is selected.
<Button Grid.Row="4" Grid.Column="3" x:Name="BtnEnable" Click="BtnEnable_Click"
IsEnabled="{Binding IsAxisSelected}">
We use IAxis.PowerOn to check the axis. If it is enabled, we use IAxis.Power to disable it; if it is disabled, we enable it. Because an axis needs some time to become enabled or disabled, we give IAxis.Power 5,000 milliseconds to finish its job. If it is not done within the time span, the task will be cancelled.
public async Task EnableAxis()
{
if (SelectedAxis.PowerOn)
await SelectedAxis.Power(false, false, false).WaitAsync(5000, true);
else
await SelectedAxis.Power(true, true, true).WaitAsync(5000, true);
}