Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在UIElement'上模拟事件;未在Windows Phone 7上继承的_C#_.net_Windows Phone 7 - Fatal编程技术网

C# 在UIElement'上模拟事件;未在Windows Phone 7上继承的

C# 在UIElement'上模拟事件;未在Windows Phone 7上继承的,c#,.net,windows-phone-7,C#,.net,Windows Phone 7,我有一个非常简单的要求,即我想向UIElement发送一个合成事件,在本例中是一个网格。我想要的只是能够将合成事件发送到UIElement 比如说, StackPanel myPanel; StackPanel topPanel; topPanel.MouseLeftButtonUp += new MouseButtonEventHandler(topPanel_MouseLeftButtonUp); private void topPanel_MouseLeftButtonUp(objec

我有一个非常简单的要求,即我想向UIElement发送一个合成事件,在本例中是一个网格。我想要的只是能够将合成事件发送到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的实例上调用它。那么,如何在现有类上发送合成事件呢

注:我不能创建自定义继承类的原因是当前的代码库太大,如果我采用这种方法,需要进行的更改数量是不可行的

非常感谢您的帮助

问候,,
roahn

您可以将相关代码从事件处理程序移动到方法,而不是引发事件。然后,只要想模拟按钮单击,就可以调用该方法。但是,如果要模拟在图元上单击按钮,可以执行以下操作:

//Assuming myPanel_MouseLeftButtonUp is the event handler for myPanel
myPanel_MouseLeftButtonUp(null, null);

问题是,我不能只调用事件处理程序,因为我依赖的是StackPanel上的事件会冒泡到StackPanel包含的其他UIElement,我关心的是它们的事件处理程序。此外,这些对象是动态生成的,因此依赖于“sender”属性,该属性的设置取决于StackPanel的哪个子对象被准确单击。