背景:我正在使用 WPF 和 C# (3.5),并且正在开发一个应用程序,该应用程序允许用户查看已经是已编译程序集一部分的表单/窗口/用户控件。当他们查看它时,他们应该能够单击任何控件(按钮、文本框,甚至标签),控件旁边应该出现一个小的弹出编辑器,然后他们可以在其中输入该控件的工具提示、helpID 等。

它的长处和短处:我需要模仿 WPF 中的基本设计视图。这意味着我至少需要执行以下操作:

  • 从给定的程序集中加载用户控件/窗口 (没问题)
  • 将其实例化为用户控件/窗口 (没问题)
  • 清除其所有控件的所有订阅的 EventHandler
  • 将我自己的“ShowEditorPopup”EventHandler 分配给每个控件 (应该不是问题)

首先,如果有人对更简单或更好的路线有建议,请告诉我。(显然 WPF 没有 DesignHost 类型的组件(就像我读过的 .NET 2 那样),所以就这样了。)

我被困在粗体项目上 - 清除任何订阅的事件处理程序。在深入研究并进入 Reflector 后,我想出了这段很酷的危险代码(在这里,我只是试图获取 XAML 中定义的名为 someButton 的单个按钮的所有事件处理程序):

<Button Name="someButton" Click="someButton_Click"/>

这是代码(如果需要,您可以从 someButton_Click 事件处理程序运行它):

public void SomeFunction()
{
// Get the control's Type
Type someButtonType = ((UIElement)someButton).GetType();

// Dig out the undocumented (yes, I know, it's risky) EventHandlerStore
// from the control's Type
PropertyInfo EventHandlersStoreType =  
        someButtonType.GetProperty("EventHandlersStore",  
        BindingFlags.Instance | BindingFlags.NonPublic);

// Get the actual "value" of the store, not just the reflected PropertyInfo
Object EventHandlersStore = EventHandlersStoreType.GetValue(someButton, null);

// Get the store's type ...
Type storeType = EventHandlersStore.GetType();

// ... so we can pull out the store's public method GetRoutedEventHandlers
MethodInfo GetEventHandlers =  
        storeType.GetMethod("GetRoutedEventHandlers",  
        BindingFlags.Instance | BindingFlags.Public);

// Reflector shows us that the method's sig is this:
// public RoutedEventHandlerInfo[] GetRoutedEventHandlers(RoutedEvent routedEvent);

// So set up the RoutedEvent param
object[] Params = new object[] { ButtonBase.ClickEvent as RoutedEvent };
// I've also seen this for the param, but doesn't seem to make a difference:
// object[] Params = new object[] { someButton.ClickEvent };

// And invoke it ... and watch it crash!
GetEventHandlers.Invoke(someButton, Params);
}

它直到 Invoke 为止,它返回:对象与目标类型不匹配(即我的参数或目标对象混乱)。我发现你可以用以下方法解决这个问题:

GetEventHandlers.Invoke(Activator.CreateInstance(someButton.GetType()), Params);
// Also doesn't work...

当我在 GetEventHandlers MethodInfo 上设置监视时,它看起来很棒,只是不喜欢我在调用 Invoke 时传递给它的内容。

我觉得我正处于如何获取 RoutedEvent 处理程序列表的最后一步(就像旧的 GetInitationList() 一样,显然它不适用于 WPF RoutedEvents)。从那里开始,从每个控件中删除这些处理程序并获得无事件形式就足够简单了,然后我可以向其中添加自己的事件。

有什么线索吗?再次强调,如果有更好/更简单的方法来完成整个任务,请告诉我:)

有帮助吗?

解决方案

如果你采取不同的方法怎么办?你可以打电话 EventManager.RegisterClassHandler() 对于所有事件,然后在处理程序中(假设该事件是针对设计图面上的控件,而不是 UI 的一部分)将事件标记为已处理。这应该可以防止它被转发到设计表面上的控件,因为类处理程序是在标准事件处理程序之前调用的。

您仍然需要使用反射来获取控件提供的事件列表,但至少这样您就不会使用反射来删除事件。另外,如果您加载的程序集还注册了一个类处理程序(可能在您的代码之前),那么它们的处理程序将首先被调用,但我猜这种情况很少发生。

其他提示

如果你使用 GetEventHandlers.Invoke(EventHandlersStore , Params) 它似乎运行良好并且不会崩溃。

使用上面的代码我这样做了:

// Get the control's Type
Type controlViewType = ((UIElement)control).GetType();

// Dig out the undocumented (yes, I know, it's risky) EventHandlerStore
// from the control's Type
PropertyInfo EventHandlersStoreType =
controlViewType.GetProperty("EventHandlersStore",
BindingFlags.Instance | BindingFlags.NonPublic);

// Get the actual "value" of the store, not just the reflected PropertyInfo
Object EventHandlersStore = EventHandlersStoreType.GetValue(tree, null);
var miGetRoutedEventHandlers 
EventHandlersStore.GetType().GetMethod("GetRoutedEventHandlers", 
BindingFlags.Public | BindingFlags.Instance);
RoutedEventHandlerInfo[] res =   
(RoutedEventHandlerInfo[])miGetRoutedEventHandlers.Invoke(EventHandlersStore, 
new object[] { CheckedTreeViewItem.CheckedEvent });

一旦你有了它,那么唯一的问题是你现在有了方法信息,所以你需要获取实现该方法的对象的实例。通常事件处理程序是在 Window 或 Page 对象上定义的。所以要得到它:var 父 = VisualTreeHelper.GetParent(control);while(!(控制是窗口)&&!(控件是页面)){parent = visualTreeHelper.getParent(parent);}

使用该实例,您可以使用以下命令调用事件:

res.[0].Handler.Method.Invoke(parent, new object[] { control, new RoutedEventArgs() }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top