模拟视觉拖/放(此代码有什么问题?)

发布于 2024-11-01 09:36:52 字数 2035 浏览 3 评论 0原文

我有一个 WPF 4/VB.net 2010 项目,我正在尝试进行可视化“拖放”(让一个对象在 MouseUp 上跟随鼠标并在 MouseDown 上“粘住”。)我有以下代码:

Private Sub Tile1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles Tile1.MouseDown
    Tile1.CaptureMouse()
    IsDragging = True
End Sub

Private Sub Tile1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Input.MouseEventArgs) Handles Tile1.MouseMove
    If IsDragging = True Then
        Dim canvPosToWindow As Point = canv.TransformToAncestor(Me).Transform(New Point(0, 0))

        Dim r As Rectangle = TryCast(sender, Rectangle)
        Dim upperlimit As Double = canvPosToWindow.Y + (r.Height / 2)
        Dim lowerlimit As Double = canvPosToWindow.Y + canv.ActualHeight - (r.Height / 2)

        Dim leftlimit As Double = canvPosToWindow.X + (r.Width / 2)
        Dim rightlimit As Double = canvPosToWindow.X + canv.ActualWidth - (r.Width / 2)

        Dim absmouseXpos As Double = e.GetPosition(Me).X
        Dim absmouseYpos As Double = e.GetPosition(Me).Y

        If (absmouseXpos > leftlimit AndAlso absmouseXpos < rightlimit) AndAlso (absmouseYpos > upperlimit AndAlso absmouseYpos < lowerlimit) Then
            r.SetValue(Canvas.LeftProperty, e.GetPosition(canv).X - (r.Width / 2))
            r.SetValue(Canvas.TopProperty, e.GetPosition(canv).Y - (r.Height / 2))
        End If
    End If
End Sub

Private Sub Tile1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles Tile1.MouseUp
    Tile1.ReleaseMouseCapture()
    IsDragging = False
End Sub

我收到此错误:

NullReferenceException,未将对象引用设置为对象的实例。

在以下每一行中:

 Dim upperlimit As Double = canvPosToWindow.Y + (r.Height / 2)
 Dim lowerlimit As Double = canvPosToWindow.Y + canv.ActualHeight - (r.Height / 2)

 Dim leftlimit As Double = canvPosToWindow.X + (r.Width / 2)
 Dim rightlimit As Double = canvPosToWindow.X + canv.ActualWidth - (r.Width / 2)

我做错了什么?

I have a WPF 4/VB.net 2010 project, and I'm trying to do a visual "drag and drop" (having an object follow the mouse on MouseUp and "stick" on MouseDown.) I have the following code behind:

Private Sub Tile1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles Tile1.MouseDown
    Tile1.CaptureMouse()
    IsDragging = True
End Sub

Private Sub Tile1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Input.MouseEventArgs) Handles Tile1.MouseMove
    If IsDragging = True Then
        Dim canvPosToWindow As Point = canv.TransformToAncestor(Me).Transform(New Point(0, 0))

        Dim r As Rectangle = TryCast(sender, Rectangle)
        Dim upperlimit As Double = canvPosToWindow.Y + (r.Height / 2)
        Dim lowerlimit As Double = canvPosToWindow.Y + canv.ActualHeight - (r.Height / 2)

        Dim leftlimit As Double = canvPosToWindow.X + (r.Width / 2)
        Dim rightlimit As Double = canvPosToWindow.X + canv.ActualWidth - (r.Width / 2)

        Dim absmouseXpos As Double = e.GetPosition(Me).X
        Dim absmouseYpos As Double = e.GetPosition(Me).Y

        If (absmouseXpos > leftlimit AndAlso absmouseXpos < rightlimit) AndAlso (absmouseYpos > upperlimit AndAlso absmouseYpos < lowerlimit) Then
            r.SetValue(Canvas.LeftProperty, e.GetPosition(canv).X - (r.Width / 2))
            r.SetValue(Canvas.TopProperty, e.GetPosition(canv).Y - (r.Height / 2))
        End If
    End If
End Sub

Private Sub Tile1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles Tile1.MouseUp
    Tile1.ReleaseMouseCapture()
    IsDragging = False
End Sub

I am getting this error:

NullReferenceException, Object reference not set to an instance of an object.

On each of the following lines:

 Dim upperlimit As Double = canvPosToWindow.Y + (r.Height / 2)
 Dim lowerlimit As Double = canvPosToWindow.Y + canv.ActualHeight - (r.Height / 2)

 Dim leftlimit As Double = canvPosToWindow.X + (r.Width / 2)
 Dim rightlimit As Double = canvPosToWindow.X + canv.ActualWidth - (r.Width / 2)

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若能看破又如何 2024-11-08 09:36:52

我怀疑这个语句使 r “空”而不是一个正确的矩形。

Dim r As Rectangle = TryCast(sender, Rectangle)

发送者可能不是一个矩形,而可能是一个 WPF-Control(System.Windows.Controls.Control 的某个子类)。

您可以通过在该行上放置一个调试点来轻松地使用 Visual Studio 调试器进行检查,并查看该语句是否计算结果。

祝您的拖放实施顺利:)。

I suspect that this statement makes r "null" and not a proper rectangle.

Dim r As Rectangle = TryCast(sender, Rectangle)

The sender is probably not a rectangle but probably a WPF-Control (some subclass of System.Windows.Controls.Control).

You can easily check with the Visual Studio debugger, by placing a debug-point on this line and see if this statement evaluates.

Good luck with your drag-drop implementation :).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文