The UIApplication object also has a few events that programs can listen to. For example, when a Revit window (term DialogBox in API) is showing up, the DialogBoxShowing event will be fired. The other events are ApplicationClosing, Idling, ViewActivated, and ViewActivating. The Idling one may be found very useful in some situations.
Let’s take the DialogBoxShowing event for an example. We define a handler for it first:
public static void UIAppEvent_DialogBoxShowing_Handler(Object sender, EventArgs args)
{
MessageBox.Show("DialogBoxShowing");
}
If you’d like to access some particular properties of the event, you need to cast the generic EventArgs type to the specific DialogBoxShowingEventArgs:
DialogBoxShowingEventArgs specArgs = args as DialogBoxShowingEventArgs;
Next, we hook up the handler into the system through the OnStartup implementation of an IExternalApplication interface:
public Result OnStartup(UIControlledApplication uiApp)
{
//……
uiApp.DialogBoxShowing += UIAppEventHandlers1.UIAppEvent_DialogBoxShowing_Handler;
//……
}
We remove the hander in the OnShutdown callback of the same external application:
public Result OnShutdown(UIControlledApplication uiApp)
{
//……
uiApp.DialogBoxShowing -= UIAppEventHandlers1.UIAppEvent_DialogBoxShowing_Handler;
//……
}
The code examples regarding handling, registration and un-registration of all the UIApplication events have been appended below for reference:
UIApplication event handlers:
using System;
using System.Text;
using System.Xml;
using System.Linq;
using System.Reflection;
using System.ComponentModel;
using System.Collections;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Forms;
using System.IO;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Events;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.DB.Structure;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.UI.Events;
using Autodesk.Revit.Collections;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.Utility;
using RvtApplication = Autodesk.Revit.ApplicationServices.Application;
using RvtDocument = Autodesk.Revit.DB.Document;
namespace RevitAddinCSProject
{
public class UIAppEventHandlers1
{
public static void UIAppEvent_ApplicationClosing_Handler(Object sender, EventArgs args)
{
MessageBox.Show("ApplicationClosing");
}
public static void UIAppEvent_DialogBoxShowing_Handler(Object sender, EventArgs args)
{
MessageBox.Show("DialogBoxShowing");
}
public static void UIAppEvent_Idling_Handler(Object sender, EventArgs args)
{
//MessageBox.Show("Idling");
}
public static void UIAppEvent_ViewActivated_Handler(Object sender, EventArgs args)
{
MessageBox.Show("ViewActivated");
}
public static void UIAppEvent_ViewActivating_Handler(Object sender, EventArgs args)
{
MessageBox.Show("ViewActivating");
}
}
}
UIApplication event registration in the OnStartup implementation of an IExternalApplication interface:
public Result OnStartup(UIControlledApplication uiApp)
{
try
{
//……
uiApp.ApplicationClosing += UIAppEventHandlers1.UIAppEvent_ApplicationClosing_Handler;
uiApp.DialogBoxShowing += UIAppEventHandlers1.UIAppEvent_DialogBoxShowing_Handler;
uiApp.Idling += UIAppEventHandlers1.UIAppEvent_Idling_Handler;
uiApp.ViewActivated += UIAppEventHandlers1.UIAppEvent_ViewActivated_Handler;
uiApp.ViewActivating += UIAppEventHandlers1.UIAppEvent_ViewActivating_Handler;
return Result.Succeeded;
}
catch (Exception ex)
{
MessageBox.Show( ex.ToString() );
return Result.Failed;
}
}
UIApplication event un-registration in the OnShutdown implementation of an IExternalApplication interface:
public Result OnShutdown(UIControlledApplication uiApp)
{
try
{
//……
uiApp.ApplicationClosing -= UIAppEventHandlers1.UIAppEvent_ApplicationClosing_Handler;
uiApp.DialogBoxShowing -= UIAppEventHandlers1.UIAppEvent_DialogBoxShowing_Handler;
uiApp.Idling -= UIAppEventHandlers1.UIAppEvent_Idling_Handler;
uiApp.ViewActivated -= UIAppEventHandlers1.UIAppEvent_ViewActivated_Handler;
uiApp.ViewActivating -= UIAppEventHandlers1.UIAppEvent_ViewActivating_Handler;
return Result.Succeeded;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
return Result.Failed;
}
}
It takes a while to set all these up and sort out some details manually. The good news is that all these can be done automatically by the RevitAddinWizard in a moment.
The steps are the same as introduced and demonstrated earlier in another article, Manage Revit Application Events with C#.
Links to some related articles:
Manage Revit Application Events of Revit API
Use RevitAddinWizard to Add Revit Application Event Handlers of Revit API
Implement Revit FailuresProcessing Event Hanlders of Revit API
Use RevitAddinWizard to Implement Revit FailuresProcessing Event Hanlders of Revit API
Manage Revit UIApplication Events
Manage Revit Document Events of Revit API - 3 Document Event Handler of RevitAddinWizard
Recent Comments