- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hello Revit API friends,
I have a general question about handling deactivating and reactivating a ribbon button.
I want to deactivate the ribbon button while the command is running, so it can´t be run multiple times.
But I´m not quite sure what´s the best way to make sure the button is reactivated in any case that can happen.
Please see the following simple example of a command that is starting a wpf form.
- I use the IExternalCommandAvailability right at the start of my command.
- I use a closing eventhandler that triggers the IExternalCommandAvailability if the form is closed.
- I use IExternalCommandAvailability in a catch block.
Is this a 100% save method to amke sure my button is set active again also if error occure in my command? Are there other options for this? Can I check somehow if a command is running or if a wpf form is open?
Unfortunately I can not use a finally block because it will run instantly as the command seems to run async.
Appreciate any help on this topic!
Best regards!
[Transaction(TransactionMode.Manual)]
internal class Command : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
try
{
CommandAvailability.IsCommandRunning = true;
var mainWindow = new MainWindow(commandData.Application);
mainWindow.Show();
mainWindow.Closed += (s, e) => CommandAvailability.IsCommandRunning = false;
return Result.Succeeded;
}
catch
{
CommandAvailability.IsCommandRunning = false;
return Result.Succeeded;
}
}
}
public class CommandAvailability : IExternalCommandAvailability
{
private static bool isCommandRunning = false;
public static bool IsCommandRunning
{
get { return isCommandRunning; }
set { isCommandRunning = value; }
}
public bool IsCommandAvailable(UIApplication applicationData, CategorySet selectedCategories)
{
return !isCommandRunning;
}
}
Solved! Go to Solution.