Hi Dears,
I'm trying to register/unregister to Revit View Activated event from my user control not from the main class witch inherit from IExternalApplication because my add-in consist of more than one file and i want to do that in some locations only
my code is
public partial class ViewsUserControl : UserControl { private ViewsVM _ViewsVM = new ViewsVM(); public ViewsUserControl() { InitializeComponent(); DataContext = _ViewsVM; Unloaded += ViewsUserControl_Unloaded; RevitProperties.uiApp.ViewActivated += UiApp_ViewActivated; } private void ViewsUserControl_Unloaded(object sender, RoutedEventArgs e) { RevitProperties.uiApp.ViewActivated -= UiApp_ViewActivated; } private void UiApp_ViewActivated(object sender, ViewActivatedEventArgs e) { MessageBox.Show("test"); }
}
}
in the previous code, when unload event fires, it gives error 'invalid call to Revit API Revit is currently not within an API context'
how can I unregister the Revit event when the add-in closed? any idea on how to do that?
thanks for your support and your time
Solved! Go to Solution.
Solved by RPTHOMAS108. Go to Solution.
You will have to use an interface that gives you a context either:
IExternalCommand
IExternalApplication
IExternalEventHandler
Not clear on what you mean by many different files in terms of how you can't register during OnStartup. Putting this kind of logic into a user control seems to be the wrong approach i.e. should user control subscribe to outside events internally? Seems better to have an outside event calling something on the UserControl specific to the UserControl or raising a UserControl event.
I'm using already
IExternalCommand
IExternalApplication
IExternalEventHandler
to have button and to run my add-in.
my add-in have many windows, one of them using some Revit events, so I need to register those events and unregister them in the window it self not in Onstartup() or OnShutdown() methods
You can use IExternalEventHandler then? It gives you an instance of UIApplication doesn't it?
Wherever you subscribe you can unsubscribe but for above you would need to raise an ExternalEvent to get the object.
I think there are better options however. When window closes call static function that removes handler from where it was created. This isn't even about RevitAPI it is just events in general and design patterns MVVM/MVC etc.
If you have eventing logic that raises events based on API events created during OnStartup then couldn't your Windows subscribe/unsubscribe to these secondary events at will (as long as you isolate objects passed from event args)? Events leading to other events that's common. If you have 100 windows needing to know if a View is activating you still only need one ViewActivating event subscription during OnStartup.
@RPTHOMAS108 thanks dear, you gave some ideas and they look good but I'm not sure how to implement
You can use IExternalEventHandler then? It gives you an instance of UIApplication doesn't it?
I'm already having instance to get it from common data variable
Wherever you subscribe you can unsubscribe but for above you would need to raise an ExternalEvent to get the object.
I can register Revit events any where without using external events and its working, the problem in unregistering it, I don't know how to unregister it in external event
I think there are better options however. When window closes call static function that removes handler from where it was created. This isn't even about RevitAPI it is just events in general and design patterns MVVM/MVC etc.
can you give me any example please
If you have eventing logic that raises events based on API events created during OnStartup then couldn't your Windows subscribe/unsubscribe to these secondary events at will (as long as you isolate objects passed from event args)? Events leading to other events that's common. If you have 100 windows needing to know if a View is activating you still only need one View Activating event subscription during OnStartup.
this sounds good also, but I'm not sure how to implement it
my main problem is to unregister the event on closing my add-in internal windows or user controls
many thanks for your help
dear @RPTHOMAS108
you were right, I have created an attached property for unloaded event and create external event in it
rise the external event form the attached property, unsubscribe code in the external event class it self
too much code for single line code , I don't know why Revit is so much difficult
many thanks for lighting up my way
I Hope if You have sometime to advice me about the your third and fourth methods that you have mentioned in your replay, I think they will be more better
many thanks in advance
Hi Hazem
I’ve got same problem on unregistering the viewactivated Event. I’m registering the activatedView into an External évent And with a property set.
On loading my modeless form I do à Raise() but when I do an other Raise() on My formClosing Event does not execute The externalEvent.
but when I do à raise into a btn_click It works....
Thx for your help
Can't find what you're looking for? Ask the community or share your knowledge.