【问题标题】:Loss of Silverlight mouse up events after mouse capture?鼠标捕获后 Silverlight 鼠标启动事件丢失?
【发布时间】:2010-07-16 13:42:21
【问题描述】:

我创建了一个非常简单的测试控件,它在画布上(在其他容器中,但无关紧要)上有一个RectangleRectangle 具有鼠标按下、鼠标移动和鼠标向上的事件处理程序。如果我在 Rectangle 的 MouseLeftButtonDown 事件中捕获鼠标,我不会收到相应的 MouseLeftButtonUp 事件。

一些代码:

private void rect1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    if (_captured = CaptureMouse())
    {
        _offset = new Point(Canvas.GetLeft(rect1), Canvas.GetTop(rect1));
        _origin = e.GetPosition(RootCanvas);
        e.Handled = true;
    }
}


private void rect1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
    if (_captured)
    {
        ReleaseMouseCapture();
        _captured = false;
        e.Handled = true;
    }
}

我还为容器元素附加了事件处理程序,只是为了确保其中一个没有以某种方式获得鼠标向上事件,但它们都没有。 Silverlight 中是否有我尚未了解的预期?

【问题讨论】:

  • 我注意到我正在开发的 WPF 应用程序中存在类似问题,但在按下其他鼠标按钮时尝试捕获鼠标时忽略了它。意识到除了左键之外它对其他任何东西都不起作用后,我放弃了这种方法并且没有进一步调查这个问题。

标签: silverlight user-controls mouse


【解决方案1】:

我认为您对实际捕获鼠标事件的内容有点困惑。

在你这样做的时候考虑一​​下:-

 if (_captured = CaptureMouse())

CaptureMouse 实际被调用的对象是什么?

Answer:您的代码作为代码隐藏的用户控件。如果你想让矩形捕捉你会做的鼠标:-

 if (_captured = rect1.CaptureMouse())

【讨论】:

  • 与其说是不注意,不如说是困惑。这是我一天中的面部护理时刻。感谢您指出应该很明显的内容。
【解决方案2】:

捕捉鼠标();从 mouseDown 事件中再尝试。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-19
    • 2016-12-26
    • 2016-04-19
    • 1970-01-01
    • 1970-01-01
    • 2012-12-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多