在UIElement上模拟事件而不在Windows Phone 7上继承 [英] Simulating events on UIElement's without inheriting on Windows Phone 7
问题描述
我有一个非常简单的要求,即我想向UIElement(在这种情况下为Grid)发送综合事件.我想要的只是能够将综合事件发送到UIElement.
I have a very simple requirement, i.e. I want to send a synthetic event to a UIElement, in this case, a Grid. What I want is simply that I be able to send a synthetic event to an UIElement.
例如,
StackPanel myPanel;
StackPanel topPanel;
topPanel.MouseLeftButtonUp += new MouseButtonEventHandler(topPanel_MouseLeftButtonUp);
private void topPanel_MouseLeftButtonUp(object sender, MouseEventArgs args) {
// Here I want to send the MouseLeftButtonUp event to myPanel
}
可以使用 RaiseEvent
,但这是一个受保护的事件,因此我不能仅在任何UIElement的实例上调用它.那么我该如何在现有的类上发送综合事件呢?
It is possible using RaiseEvent
, but it is a protected event and hence I cannot just call it on an instance of any UIElement. So how do I go about sending a synthetic event on existing classes?
P.S:之所以无法创建自定义的继承类,是因为当前的代码库太大,并且在采用这种方法的情况下需要进行的更改数量不可行.
P.S: The reason that I cannot create custom inherited classes is that the current code base is too huge and the number of changes that will be required in case I take such an approach are not feasible.
非常感谢您的帮助.
关于,罗恩
推荐答案
除了引发事件,您还可以将相关代码从事件处理程序移至方法.然后,只要您想模拟按钮单击,就可以调用该方法.但是,如果要模拟按钮在元素上的单击,则可以执行以下操作:
Instead of raising events, you could just move the relevant code from the event handler to a method. Then, you can just call the method whenever you want to simulate a button click. However, if you want to simulate the button click on an element, you could do this:
//Assuming myPanel_MouseLeftButtonUp is the event handler for myPanel
myPanel_MouseLeftButtonUp(null, null);
这篇关于在UIElement上模拟事件而不在Windows Phone 7上继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!