Background: I use WPF and C # (3.5) and work on an application that allows the user to view the form / window / usercontrol, which are already part of the compiled assembly. When they see it, they should be able to click on any control (buttons, text fields and even labels), a small pop-up editor should appear for this control, where they can enter a hint, helpID, etc. For this control.
Long and short: I need to emulate the basic design in WPF. This means that I need to do at least the following:
- Download usercontrol / window from this assembly (no problem)
- Create a usercontrol / window file (no problem)
- Clear all signed EventHandlers for all its controls
- Assign my own "ShowEditorPopup" EventHandler to each control (should not be a problem).
First of all, if anyone has suggestions for an easier or better route, please let me know. (Apparently, for WPF, there is no DesignHost component component (like, for example, I read .NET 2), so go out.)
I am stuck on a highlighted item - releasing all registered EventHandlers. After digging around some and getting into Reflector, I came up with this cool piece of dangerous code (here I'm just trying to get all the EventHandlers for a single button named someButton defined in XAML):
<Button Name="someButton" Click="someButton_Click"/>
Here's the code (you can run it from someButton_Click eventHandler if you want):
public void SomeFunction()
{
Type someButtonType = ((UIElement)someButton).GetType();
PropertyInfo EventHandlersStoreType =
someButtonType.GetProperty("EventHandlersStore",
BindingFlags.Instance | BindingFlags.NonPublic);
Object EventHandlersStore = EventHandlersStoreType.GetValue(someButton, null);
Type storeType = EventHandlersStore.GetType();
MethodInfo GetEventHandlers =
storeType.GetMethod("GetRoutedEventHandlers",
BindingFlags.Instance | BindingFlags.Public);
object[] Params = new object[] { ButtonBase.ClickEvent as RoutedEvent };
GetEventHandlers.Invoke(someButton, Params);
}
Invoke, : Object (.. ). , :
GetEventHandlers.Invoke(Activator.CreateInstance(someButton.GetType()), Params);
GetEventHandlers MethodInfo, , , , Invoke.
, , RoutedEvent (, GetInvocationList(), , -, WPF RoutedEvents). , , .
? , / , :)