How do I Manually Invoke/Raise Mouse Events in Silverlight 4?

元气小坏坏 提交于 2019-12-11 00:04:41

问题


In Silverlight 4, I wish to invoke one of the mouse button click events when the right mouse button is clicked. I have the RightMouseButtonDown click event wired up, but I don't know how to manually fire the MouseLeftButtonUp event.

I have tried raising the event in the following fashion.

private void MainLayoutRootMouseButton(object sender, MouseButtonEventArgs e)
{
    MouseLeftButtonDown(sender, e);
}

However, a compiler error occurs:

"The event 'System.Windows.UIElement.MouseLeftButtonDown' can only appear on the left hand side of += or -=

What do I need to do to manually raise this event?


回答1:


The other answers are correct in that they show you how to call those same code paths as you have for the other mouse events.

It should be clear that in Silverlight, you cannot raise (or automate) actual mouse button clicks, for security reasons.

Only user initiated actions such as the actual mouse moving can create real MouseEventArgs and fire the handlers directly, through the Silverlight input system.




回答2:


You really shouldn't be raising the event itself. Instead, make the code inside the MouseLeftButtonUp its own function and then call that function from both the MouseLeftButtonUp and MouseRightButtonUp events.

e.g.

private void MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    DoTheSameThing(sender, e);
}

private void MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
    DoTheSameThing(sender, e);
}

private void DoTheSameThing(object sender, MouseButtonEventArgs e)
{
    //Handle left or right mouse buttons up event
}



回答3:


In your page's init function you need to have this line of code

someObjectOnPage.MouseLeftButtonDown += new MouseDownEventHandler(DoSomething);
someObjectOnPage.MouseRightButtonDown += new MouseDownEventHandler(DoSomething);

This is pure psuedo code but should lead you on the right track

also your method will need to look like this

void DoSomething(object sender, MouseButtonEventArgs e) { }


来源:https://stackoverflow.com/questions/3250604/how-do-i-manually-invoke-raise-mouse-events-in-silverlight-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!